apm.c revision 14608
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@mt.cs.keio.ac.jp> 6 * 7 * This software may be used, modified, copied, and distributed, in 8 * both source and binary form provided that the above copyright and 9 * these terms are retained. Under no circumstances is the author 10 * responsible for the proper functioning of this software, nor does 11 * the author assume any responsibility for damages incurred with its 12 * use. 13 * 14 * Sep, 1994 Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD) 15 * 16 * $Id: apm.c,v 1.26 1996/03/12 21:51:58 nate Exp $ 17 */ 18 19#include "apm.h" 20 21#include <sys/param.h> 22#include <sys/conf.h> 23#include <sys/kernel.h> 24#ifdef DEVFS 25#include <sys/devfsext.h> 26#endif /*DEVFS*/ 27#include <sys/systm.h> 28#include <sys/malloc.h> 29#include <sys/ioctl.h> 30#include <sys/file.h> 31#include <sys/proc.h> 32#include <sys/vnode.h> 33#include "i386/isa/isa.h" 34#include "i386/isa/isa_device.h" 35#include <machine/apm_bios.h> 36#include <machine/segments.h> 37#include <machine/clock.h> 38#include <vm/vm.h> 39#include <vm/vm_param.h> 40#include <vm/pmap.h> 41#include <sys/syslog.h> 42#include "apm_setup.h" 43 44static int apm_display_off __P((void)); 45static int apm_int __P((u_long *eax, u_long *ebx, u_long *ecx)); 46static void apm_resume __P((void)); 47 48/* static data */ 49struct apm_softc { 50 int initialized, active, halt_cpu; 51 u_int minorversion, majorversion; 52 u_int cs32_base, cs16_base, ds_base; 53 u_int cs_limit, ds_limit; 54 u_int cs_entry; 55 u_int intversion; 56 int idle_cpu, disabled, disengaged; 57 struct apmhook sc_suspend; 58 struct apmhook sc_resume; 59 void *sc_devfs_token; 60}; 61 62static struct apm_softc apm_softc[NAPM]; 63static struct apm_softc *master_softc = NULL; /* XXX */ 64static struct apmhook *hook[NAPM_HOOK]; /* XXX */ 65 66#define is_enabled(foo) ((foo) ? "enabled" : "disabled") 67 68/* Map version number to integer (keeps ordering of version numbers) */ 69#define INTVERSION(major, minor) ((major)*100 + (minor)) 70 71static timeout_t apm_timeout; 72static d_open_t apmopen; 73static d_close_t apmclose; 74static d_ioctl_t apmioctl; 75 76#define CDEV_MAJOR 39 77static struct cdevsw apm_cdevsw = 78 { apmopen, apmclose, noread, nowrite, /*39*/ 79 apmioctl, nostop, nullreset, nodevtotty,/* APM */ 80 seltrue, nommap, NULL , "apm" ,NULL, -1}; 81 82/* setup APM GDT discriptors */ 83static void 84setup_apm_gdt(u_int code32_base, u_int code16_base, u_int data_base, u_int code_limit, u_int data_limit) 85{ 86 /* setup 32bit code segment */ 87 gdt_segs[GAPMCODE32_SEL].ssd_base = code32_base; 88 gdt_segs[GAPMCODE32_SEL].ssd_limit = code_limit; 89 90 /* setup 16bit code segment */ 91 gdt_segs[GAPMCODE16_SEL].ssd_base = code16_base; 92 gdt_segs[GAPMCODE16_SEL].ssd_limit = code_limit; 93 94 /* setup data segment */ 95 gdt_segs[GAPMDATA_SEL ].ssd_base = data_base; 96 gdt_segs[GAPMDATA_SEL ].ssd_limit = data_limit; 97 98 /* reflect these changes on physical GDT */ 99 ssdtosd(gdt_segs + GAPMCODE32_SEL, &gdt[GAPMCODE32_SEL].sd); 100 ssdtosd(gdt_segs + GAPMCODE16_SEL, &gdt[GAPMCODE16_SEL].sd); 101 ssdtosd(gdt_segs + GAPMDATA_SEL , &gdt[GAPMDATA_SEL ].sd); 102} 103 104/* 48bit far pointer */ 105static struct addr48 { 106 u_long offset; 107 u_short segment; 108} apm_addr; 109 110static int apm_errno; 111 112inline 113int 114apm_int(u_long *eax, u_long *ebx, u_long *ecx) 115{ 116 u_long cf; 117 __asm ("pushl %%ebp 118 pushl %%edx 119 pushl %%esi 120 xorl %3,%3 121 movl %3,%%esi 122 lcall _apm_addr 123 jnc 1f 124 incl %3 125 1: 126 popl %%esi 127 popl %%edx 128 popl %%ebp" 129 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=D" (cf) 130 : "0" (*eax), "1" (*ebx), "2" (*ecx) 131 ); 132 apm_errno = ((*eax) >> 8) & 0xff; 133 return cf; 134} 135 136 137/* enable/disable power management */ 138static int 139apm_enable_disable_pm(struct apm_softc *sc, int enable) 140{ 141 u_long eax, ebx, ecx; 142 143 eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM; 144 145 if (sc->intversion >= INTVERSION(1, 1)) { 146 ebx = PMDV_ALLDEV; 147 } else { 148 ebx = 0xffff; /* APM version 1.0 only */ 149 } 150 ecx = enable; 151 return apm_int(&eax, &ebx, &ecx); 152} 153 154/* Tell APM-BIOS that WE will do 1.1 and see what they say... */ 155static void 156apm_driver_version(void) 157{ 158 u_long eax, ebx, ecx; 159 160#ifdef APM_DEBUG 161 eax = (APM_BIOS<<8) | APM_INSTCHECK; 162 ebx = 0x0; 163 ecx = 0x0101; 164 i = apm_int(&eax, &ebx, &ecx); 165 printf("[%04lx %04lx %04lx %ld %02x]\n", 166 eax, ebx, ecx, i, apm_errno); 167#endif 168 169 eax = (APM_BIOS << 8) | APM_DRVVERSION; 170 ebx = 0x0; 171 ecx = 0x0101; 172 if(!apm_int(&eax, &ebx, &ecx)) 173 apm_version = eax & 0xffff; 174 175#ifdef APM_DEBUG 176 eax = (APM_BIOS << 8) | APM_INSTCHECK; 177 ebx = 0x0; 178 ecx = 0x0101; 179 i = apm_int(&eax, &ebx, &ecx); 180 printf("[%04lx %04lx %04lx %ld %02x]\n", 181 eax, ebx, ecx, i, apm_errno); 182#endif 183} 184 185/* engage/disengage power management (APM 1.1 or later) */ 186static int 187apm_engage_disengage_pm(struct apm_softc *sc, int engage) 188{ 189 u_long eax, ebx, ecx, i; 190 191 eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM; 192 ebx = PMDV_ALLDEV; 193 ecx = engage; 194 i = apm_int(&eax, &ebx, &ecx); 195 return i; 196} 197 198/* get PM event */ 199static u_int 200apm_getevent(struct apm_softc *sc) 201{ 202 u_long eax, ebx, ecx; 203 204 eax = (APM_BIOS << 8) | APM_GETPMEVENT; 205 206 ebx = 0; 207 ecx = 0; 208 if (apm_int(&eax, &ebx, &ecx)) 209 return PMEV_NOEVENT; 210 211 return ebx & 0xffff; 212} 213 214/* suspend entire system */ 215static int 216apm_suspend_system(struct apm_softc *sc) 217{ 218 u_long eax, ebx, ecx; 219 220 eax = (APM_BIOS << 8) | APM_SETPWSTATE; 221 ebx = PMDV_ALLDEV; 222 ecx = PMST_SUSPEND; 223 224 __asm("cli"); 225 if (apm_int(&eax, &ebx, &ecx)) { 226 __asm("sti"); 227 printf("Entire system suspend failure: errcode = %ld\n", 228 0xff & (eax >> 8)); 229 return 1; 230 } 231 __asm("sti"); 232 return 0; 233} 234 235/* Display control */ 236/* 237 * Experimental implementation: My laptop machine can't handle this function 238 * If your laptop can control the display via APM, please inform me. 239 * HOSOKAWA, Tatsumi <hosokawa@mt.cs.keio.ac.jp> 240 */ 241static int 242apm_display_off(void) 243{ 244 u_long eax, ebx, ecx; 245 246 eax = (APM_BIOS << 8) | APM_SETPWSTATE; 247 ebx = PMDV_2NDSTORAGE0; 248 ecx = PMST_STANDBY; 249 if (apm_int(&eax, &ebx, &ecx)) { 250 printf("Display off failure: errcode = %ld\n", 251 0xff & (eax >> 8)); 252 return 1; 253 } 254 255 return 0; 256} 257 258/* APM Battery low handler */ 259static void 260apm_battery_low(struct apm_softc *sc) 261{ 262 printf("\007\007 * * * BATTERY IS LOW * * * \007\007"); 263} 264 265/* APM hook manager */ 266static struct apmhook * 267apm_add_hook(struct apmhook **list, struct apmhook *ah) 268{ 269 int s; 270 struct apmhook *p, *prev; 271 272#if 0 273 printf("Add hook \"%s\"\n", ah->ah_name); 274#endif 275 276 s = splhigh(); 277 if (ah == NULL) { 278 panic("illegal apm_hook!"); 279 } 280 prev = NULL; 281 for (p = *list; p != NULL; prev = p, p = p->ah_next) { 282 if (p->ah_order > ah->ah_order) { 283 break; 284 } 285 } 286 287 if (prev == NULL) { 288 ah->ah_next = *list; 289 *list = ah; 290 } else { 291 ah->ah_next = prev->ah_next; 292 prev->ah_next = ah; 293 } 294 splx(s); 295 return ah; 296} 297 298static void 299apm_del_hook(struct apmhook **list, struct apmhook *ah) 300{ 301 int s; 302 struct apmhook *p, *prev; 303 304 s = splhigh(); 305 prev = NULL; 306 for (p = *list; p != NULL; prev = p, p = p->ah_next) { 307 if (p == ah) { 308 goto deleteit; 309 } 310 } 311 panic("Tried to delete unregistered apm_hook."); 312 goto nosuchnode; 313deleteit: 314 if (prev != NULL) { 315 prev->ah_next = p->ah_next; 316 } else { 317 *list = p->ah_next; 318 } 319nosuchnode: 320 splx(s); 321} 322 323 324/* APM driver calls some functions automatically */ 325static void 326apm_execute_hook(struct apmhook *list) 327{ 328 struct apmhook *p; 329 330 for (p = list; p != NULL; p = p->ah_next) { 331#if 0 332 printf("Execute APM hook \"%s.\"\n", p->ah_name); 333#endif 334 if ((*(p->ah_fun))(p->ah_arg)) { 335 printf("Warning: APM hook \"%s\" failed", p->ah_name); 336 } 337 } 338} 339 340 341/* establish an apm hook */ 342struct 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 aip->ai_status = (u_int)sc->active; 465 466 return 0; 467} 468 469 470/* inform APM BIOS that CPU is idle */ 471void 472apm_cpu_idle(void) 473{ 474 struct apm_softc *sc = master_softc; /* XXX */ 475 476 if (sc->idle_cpu) { 477 if (sc->active) { 478 __asm ("movw $0x5305, %ax; lcall _apm_addr"); 479 } 480 } 481 /* 482 * Some APM implementation halts CPU in BIOS, whenever 483 * "CPU-idle" function are invoked, but swtch() of 484 * FreeBSD halts CPU, therefore, CPU is halted twice 485 * in the sched loop. It makes the interrupt latency 486 * terribly long and be able to cause a serious problem 487 * in interrupt processing. We prevent it by removing 488 * "hlt" operation from swtch() and managed it under 489 * APM driver. 490 */ 491 if (!sc->active || sc->halt_cpu) { 492 __asm("sti ; hlt"); /* wait for interrupt */ 493 } 494} 495 496/* inform APM BIOS that CPU is busy */ 497void 498apm_cpu_busy(void) 499{ 500 struct apm_softc *sc = master_softc; /* XXX */ 501 502 if (sc->idle_cpu && sc->active) { 503 __asm("movw $0x5306, %ax; lcall _apm_addr"); 504 } 505} 506 507 508/* 509 * APM timeout routine: 510 * 511 * This routine is automatically called by timer once per second. 512 */ 513 514static void 515apm_timeout(void *arg) 516{ 517 struct apm_softc *sc = arg; 518 519 apm_processevent(sc); 520 timeout(apm_timeout, (void *)sc, hz - 1 ); /* More than 1 Hz */ 521} 522 523/* enable APM BIOS */ 524static void 525apm_event_enable(struct apm_softc *sc) 526{ 527#ifdef APM_DEBUG 528 printf("called apm_event_enable()\n"); 529#endif 530 if (sc->initialized) { 531 sc->active = 1; 532 apm_timeout(sc); 533 } 534} 535 536/* disable APM BIOS */ 537static void 538apm_event_disable(struct apm_softc *sc) 539{ 540#ifdef APM_DEBUG 541 printf("called apm_event_disable()\n"); 542#endif 543 if (sc->initialized) { 544 untimeout(apm_timeout, NULL); 545 sc->active = 0; 546 } 547} 548 549/* halt CPU in scheduling loop */ 550static void 551apm_halt_cpu(struct apm_softc *sc) 552{ 553 if (sc->initialized) { 554 sc->halt_cpu = 1; 555 } 556} 557 558/* don't halt CPU in scheduling loop */ 559static void 560apm_not_halt_cpu(struct apm_softc *sc) 561{ 562 if (sc->initialized) { 563 sc->halt_cpu = 0; 564 } 565} 566 567/* device driver definitions */ 568static int apmprobe (struct isa_device *); 569static int apmattach(struct isa_device *); 570struct isa_driver apmdriver = { 571 apmprobe, apmattach, "apm" }; 572 573/* 574 * probe APM (dummy): 575 * 576 * APM probing routine is placed on locore.s and apm_init.S because 577 * this process forces the CPU to turn to real mode or V86 mode. 578 * Current version uses real mode, but on future version, we want 579 * to use V86 mode in APM initialization. 580 */ 581 582static int 583apmprobe(struct isa_device *dvp) 584{ 585 int unit = dvp->id_unit; 586 587 /* 588 * XXX - This is necessary here so that we don't panic in the idle 589 * loop because master_softc is unitialized. 590 */ 591 master_softc = &apm_softc[unit]; 592 593 switch (apm_version) { 594 case APMINI_CANTFIND: 595 /* silent */ 596 return 0; 597 case APMINI_NOT32BIT: 598 printf("apm%d: 32bit connection is not supported.\n", unit); 599 return 0; 600 case APMINI_CONNECTERR: 601 printf("apm%d: 32-bit connection error.\n", unit); 602 return 0; 603 } 604 605 return -1; 606} 607 608 609/* Process APM event */ 610static void 611apm_processevent(struct apm_softc *sc) 612{ 613 int apm_event; 614 615#ifdef APM_DEBUG 616# define OPMEV_DEBUGMESSAGE(symbol) case symbol: \ 617 printf("Original APM Event: " #symbol "\n"); 618#else 619# define OPMEV_DEBUGMESSAGE(symbol) case symbol: 620#endif 621 while (1) { 622 apm_event = apm_getevent(sc); 623 if (apm_event == PMEV_NOEVENT) 624 break; 625 switch (apm_event) { 626 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ); 627 apm_suspend(); 628 break; 629 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ); 630 apm_suspend(); 631 break; 632 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ); 633 apm_suspend(); 634 break; 635 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND); 636 apm_suspend(); 637 break; 638 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME); 639 apm_resume(); 640 break; 641 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME); 642 apm_resume(); 643 break; 644 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME); 645 apm_resume(); 646 break; 647 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW); 648 apm_battery_low(sc); 649 apm_suspend(); 650 break; 651 652 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE); 653 break; 654 655 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME); 656 inittodr(0); /* adjust time to RTC */ 657 break; 658 659 default: 660 printf("Unknown Original APM Event 0x%x\n", apm_event); 661 break; 662 } 663 } 664} 665 666/* 667 * Attach APM: 668 * 669 * Initialize APM driver (APM BIOS itself has been initialized in locore.s) 670 * 671 * Now, unless I'm mad, (not quite ruled out yet), the APM-1.1 spec is bogus: 672 * 673 * Appendix C says under the header "APM 1.0/APM 1.1 Modal BIOS Behavior" 674 * that "When an APM Driver connects with an APM 1.1 BIOS, the APM 1.1 BIOS 675 * will default to an APM 1.0 connection. After an APM Driver calls the APM 676 * Driver Version function, specifying that it supports APM 1.1, and [sic!] 677 * APM BIOS will change its behavior to an APM 1.1 connection. If the APM 678 * BIOS is an APM 1.0 BIOS, the APM Driver Version function call will fail, 679 * and the connection will remain an APM 1.0 connection." 680 * 681 * OK so I can establish a 1.0 connection, and then tell that I'm a 1.1 682 * and maybe then the BIOS will tell that it too is a 1.1. 683 * Fine. 684 * Now how will I ever get the segment-limits for instance ? There is no 685 * way I can see that I can get a 1.1 response back from an "APM Protected 686 * Mode 32-bit Interface Connect" function ??? 687 * 688 * Who made this, Intel and Microsoft ? -- How did you guess ! 689 * 690 * /phk 691 */ 692 693static int 694apmattach(struct isa_device *dvp) 695{ 696 int unit = dvp->id_unit; 697 char name[32]; 698#define APM_KERNBASE KERNBASE 699 struct apm_softc *sc = &apm_softc[unit]; 700 701 sc->initialized = 0; 702 sc->active = 0; 703 sc->halt_cpu = 1; 704 705 /* setup APM parameters */ 706 sc->cs16_base = (apm_cs32_base << 4) + APM_KERNBASE; 707 sc->cs32_base = (apm_cs16_base << 4) + APM_KERNBASE; 708 sc->ds_base = (apm_ds_base << 4) + APM_KERNBASE; 709 sc->cs_limit = apm_cs_limit; 710 sc->ds_limit = apm_ds_limit; 711 sc->cs_entry = apm_cs_entry; 712 713 sc->idle_cpu = ((apm_flags & APM_CPUIDLE_SLOW) != 0); 714 sc->disabled = ((apm_flags & APM_DISABLED) != 0); 715 sc->disengaged = ((apm_flags & APM_DISENGAGED) != 0); 716 717 /* print bootstrap messages */ 718#ifdef APM_DEBUG 719 printf(" found APM BIOS version %04x\n", apm_version); 720 printf("apm%d: Code32 0x%08x, Code16 0x%08x, Data 0x%08x\n", 721 unit, sc->cs32_base, sc->cs16_base, sc->ds_base); 722 printf("apm%d: Code entry 0x%08x, Idling CPU %s, Management %s\n", 723 unit, sc->cs_entry, is_enabled(sc->idle_cpu), 724 is_enabled(!sc->disabled)); 725 printf("apm%d: CS_limit=%x, DS_limit=%x\n", 726 unit, sc->cs_limit, sc->ds_limit); 727#endif /* APM_DEBUG */ 728 729 sc->cs_limit = 0xffff; 730 sc->ds_limit = 0xffff; 731 732 /* setup GDT */ 733 setup_apm_gdt(sc->cs32_base, sc->cs16_base, sc->ds_base, 734 sc->cs_limit, sc->ds_limit); 735 736 /* setup entry point 48bit pointer */ 737 apm_addr.segment = GSEL(GAPMCODE32_SEL, SEL_KPL); 738 apm_addr.offset = sc->cs_entry; 739 740 /* Try to kick bios into 1.1 mode */ 741 apm_driver_version(); 742 sc->minorversion = ((apm_version & 0x00f0) >> 4) * 10 + 743 ((apm_version & 0x000f) >> 0); 744 sc->majorversion = ((apm_version & 0xf000) >> 12) * 10 + 745 ((apm_version & 0x0f00) >> 8); 746 747 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion); 748 749 if (sc->intversion >= INTVERSION(1, 1)) { 750 printf("apm%d: Engaged control %s\n", 751 unit, is_enabled(!sc->disengaged)); 752 } 753 754 printf(" found APM BIOS version %d.%d\n", 755 sc->majorversion, sc->minorversion); 756 printf("apm%d: Idling CPU %s\n", unit, is_enabled(sc->idle_cpu)); 757 758 /* enable power management */ 759 if (sc->disabled) { 760 if (apm_enable_disable_pm(sc, 1)) { 761 printf("Warning: APM enable function failed! [%x]\n", 762 apm_errno); 763 } 764 } 765 766 /* engage power managment (APM 1.1 or later) */ 767 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) { 768 if (apm_engage_disengage_pm(sc, 1)) { 769 printf("Warning: APM engage function failed [%x]\n", 770 apm_errno); 771 } 772 } 773 774 /* default suspend hook */ 775 sc->sc_suspend.ah_fun = apm_default_suspend; 776 sc->sc_suspend.ah_arg = sc; 777 sc->sc_suspend.ah_name = "default suspend"; 778 sc->sc_suspend.ah_order = APM_MAX_ORDER; 779 780 /* default resume hook */ 781 sc->sc_resume.ah_fun = apm_default_resume; 782 sc->sc_resume.ah_arg = sc; 783 sc->sc_resume.ah_name = "default resume"; 784 sc->sc_resume.ah_order = APM_MIN_ORDER; 785 786 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend); 787 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume); 788 789 apm_event_enable(sc); 790 791 sc->initialized = 1; 792 793#ifdef DEVFS 794 sprintf(name,"apm%d",unit); 795 sc->sc_devfs_token = devfs_add_devsw( 796 "/", name, &apm_cdevsw, unit, DV_CHR, 0, 0, 0600); 797#endif 798 return 0; 799} 800 801static int 802apmopen(dev_t dev, int flag, int fmt, struct proc *p) 803{ 804 struct apm_softc *sc = &apm_softc[minor(dev)]; 805 806 if (minor(dev) >= NAPM) { 807 return (ENXIO); 808 } 809 if (!sc->initialized) { 810 return ENXIO; 811 } 812 return 0; 813} 814 815static int 816apmclose(dev_t dev, int flag, int fmt, struct proc *p) 817{ 818 return 0; 819} 820 821static int 822apmioctl(dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p) 823{ 824 struct apm_softc *sc = &apm_softc[minor(dev)]; 825 int error = 0; 826 827#ifdef APM_DEBUG 828 printf("APM ioctl: minor = %d, cmd = 0x%x\n", minor(dev), cmd); 829#endif 830 831 if (minor(dev) >= NAPM) { 832 return ENXIO; 833 } 834 if (!sc->initialized) { 835 return ENXIO; 836 } 837 switch (cmd) { 838 case APMIO_SUSPEND: 839 apm_suspend(); 840 break; 841 case APMIO_GETINFO: 842 if (apm_get_info(sc, (apm_info_t)addr)) { 843 error = ENXIO; 844 } 845 break; 846 case APMIO_ENABLE: 847 apm_event_enable(sc); 848 break; 849 case APMIO_DISABLE: 850 apm_event_disable(sc); 851 break; 852 case APMIO_HALTCPU: 853 apm_halt_cpu(sc); 854 break; 855 case APMIO_NOTHALTCPU: 856 apm_not_halt_cpu(sc); 857 break; 858 case APMIO_DISPLAYOFF: 859 if (apm_display_off()) { 860 error = ENXIO; 861 } 862 break; 863 default: 864 error = EINVAL; 865 break; 866 } 867 return error; 868} 869 870 871static apm_devsw_installed = 0; 872 873static void 874apm_drvinit(void *unused) 875{ 876 dev_t dev; 877 878 if( ! apm_devsw_installed ) { 879 dev = makedev(CDEV_MAJOR,0); 880 cdevsw_add(&dev,&apm_cdevsw,NULL); 881 apm_devsw_installed = 1; 882 } 883} 884 885SYSINIT(apmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,apm_drvinit,NULL) 886