apm.c revision 14603
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.25 1996/03/12 06:09:34 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 return 0; 465} 466 467 468/* inform APM BIOS that CPU is idle */ 469void 470apm_cpu_idle(void) 471{ 472 struct apm_softc *sc = master_softc; /* XXX */ 473 474 if (sc->idle_cpu) { 475 if (sc->active) { 476 __asm ("movw $0x5305, %ax; lcall _apm_addr"); 477 } 478 } 479 /* 480 * Some APM implementation halts CPU in BIOS, whenever 481 * "CPU-idle" function are invoked, but swtch() of 482 * FreeBSD halts CPU, therefore, CPU is halted twice 483 * in the sched loop. It makes the interrupt latency 484 * terribly long and be able to cause a serious problem 485 * in interrupt processing. We prevent it by removing 486 * "hlt" operation from swtch() and managed it under 487 * APM driver. 488 */ 489 if (!sc->active || sc->halt_cpu) { 490 __asm("sti ; hlt"); /* wait for interrupt */ 491 } 492} 493 494/* inform APM BIOS that CPU is busy */ 495void 496apm_cpu_busy(void) 497{ 498 struct apm_softc *sc = master_softc; /* XXX */ 499 500 if (sc->idle_cpu && sc->active) { 501 __asm("movw $0x5306, %ax; lcall _apm_addr"); 502 } 503} 504 505 506/* 507 * APM timeout routine: 508 * 509 * This routine is automatically called by timer once per second. 510 */ 511 512static void 513apm_timeout(void *arg) 514{ 515 struct apm_softc *sc = arg; 516 517 apm_processevent(sc); 518 timeout(apm_timeout, (void *)sc, hz - 1 ); /* More than 1 Hz */ 519} 520 521/* enable APM BIOS */ 522static void 523apm_event_enable(struct apm_softc *sc) 524{ 525#ifdef APM_DEBUG 526 printf("called apm_event_enable()\n"); 527#endif 528 if (sc->initialized) { 529 sc->active = 1; 530 apm_timeout(sc); 531 } 532} 533 534/* disable APM BIOS */ 535static void 536apm_event_disable(struct apm_softc *sc) 537{ 538#ifdef APM_DEBUG 539 printf("called apm_event_disable()\n"); 540#endif 541 if (sc->initialized) { 542 untimeout(apm_timeout, NULL); 543 sc->active = 0; 544 } 545} 546 547/* halt CPU in scheduling loop */ 548static void 549apm_halt_cpu(struct apm_softc *sc) 550{ 551 if (sc->initialized) { 552 sc->halt_cpu = 1; 553 } 554} 555 556/* don't halt CPU in scheduling loop */ 557static void 558apm_not_halt_cpu(struct apm_softc *sc) 559{ 560 if (sc->initialized) { 561 sc->halt_cpu = 0; 562 } 563} 564 565/* device driver definitions */ 566static int apmprobe (struct isa_device *); 567static int apmattach(struct isa_device *); 568struct isa_driver apmdriver = { 569 apmprobe, apmattach, "apm" }; 570 571/* 572 * probe APM (dummy): 573 * 574 * APM probing routine is placed on locore.s and apm_init.S because 575 * this process forces the CPU to turn to real mode or V86 mode. 576 * Current version uses real mode, but on future version, we want 577 * to use V86 mode in APM initialization. 578 */ 579 580static int 581apmprobe(struct isa_device *dvp) 582{ 583 int unit = dvp->id_unit; 584 585 /* 586 * XXX - This is necessary here so that we don't panic in the idle 587 * loop because master_softc is unitialized. 588 */ 589 master_softc = &apm_softc[unit]; 590 591 switch (apm_version) { 592 case APMINI_CANTFIND: 593 /* silent */ 594 return 0; 595 case APMINI_NOT32BIT: 596 printf("apm%d: 32bit connection is not supported.\n", unit); 597 return 0; 598 case APMINI_CONNECTERR: 599 printf("apm%d: 32-bit connection error.\n", unit); 600 return 0; 601 } 602 603 return -1; 604} 605 606 607/* Process APM event */ 608static void 609apm_processevent(struct apm_softc *sc) 610{ 611 int apm_event; 612 613#ifdef APM_DEBUG 614# define OPMEV_DEBUGMESSAGE(symbol) case symbol: \ 615 printf("Original APM Event: " #symbol "\n"); 616#else 617# define OPMEV_DEBUGMESSAGE(symbol) case symbol: 618#endif 619 while (1) { 620 apm_event = apm_getevent(sc); 621 if (apm_event == PMEV_NOEVENT) 622 break; 623 switch (apm_event) { 624 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ); 625 apm_suspend(); 626 break; 627 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ); 628 apm_suspend(); 629 break; 630 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ); 631 apm_suspend(); 632 break; 633 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND); 634 apm_suspend(); 635 break; 636 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME); 637 apm_resume(); 638 break; 639 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME); 640 apm_resume(); 641 break; 642 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME); 643 apm_resume(); 644 break; 645 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW); 646 apm_battery_low(sc); 647 apm_suspend(); 648 break; 649 650 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE); 651 break; 652 653 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME); 654 inittodr(0); /* adjust time to RTC */ 655 break; 656 657 default: 658 printf("Unknown Original APM Event 0x%x\n", apm_event); 659 break; 660 } 661 } 662} 663 664/* 665 * Attach APM: 666 * 667 * Initialize APM driver (APM BIOS itself has been initialized in locore.s) 668 * 669 * Now, unless I'm mad, (not quite ruled out yet), the APM-1.1 spec is bogus: 670 * 671 * Appendix C says under the header "APM 1.0/APM 1.1 Modal BIOS Behavior" 672 * that "When an APM Driver connects with an APM 1.1 BIOS, the APM 1.1 BIOS 673 * will default to an APM 1.0 connection. After an APM Driver calls the APM 674 * Driver Version function, specifying that it supports APM 1.1, and [sic!] 675 * APM BIOS will change its behavior to an APM 1.1 connection. If the APM 676 * BIOS is an APM 1.0 BIOS, the APM Driver Version function call will fail, 677 * and the connection will remain an APM 1.0 connection." 678 * 679 * OK so I can establish a 1.0 connection, and then tell that I'm a 1.1 680 * and maybe then the BIOS will tell that it too is a 1.1. 681 * Fine. 682 * Now how will I ever get the segment-limits for instance ? There is no 683 * way I can see that I can get a 1.1 response back from an "APM Protected 684 * Mode 32-bit Interface Connect" function ??? 685 * 686 * Who made this, Intel and Microsoft ? -- How did you guess ! 687 * 688 * /phk 689 */ 690 691static int 692apmattach(struct isa_device *dvp) 693{ 694 int unit = dvp->id_unit; 695 char name[32]; 696#define APM_KERNBASE KERNBASE 697 struct apm_softc *sc = &apm_softc[unit]; 698 699 sc->initialized = 0; 700 sc->active = 0; 701 sc->halt_cpu = 1; 702 703 /* setup APM parameters */ 704 sc->cs16_base = (apm_cs32_base << 4) + APM_KERNBASE; 705 sc->cs32_base = (apm_cs16_base << 4) + APM_KERNBASE; 706 sc->ds_base = (apm_ds_base << 4) + APM_KERNBASE; 707 sc->cs_limit = apm_cs_limit; 708 sc->ds_limit = apm_ds_limit; 709 sc->cs_entry = apm_cs_entry; 710 711 sc->idle_cpu = ((apm_flags & APM_CPUIDLE_SLOW) != 0); 712 sc->disabled = ((apm_flags & APM_DISABLED) != 0); 713 sc->disengaged = ((apm_flags & APM_DISENGAGED) != 0); 714 715 /* print bootstrap messages */ 716#ifdef APM_DEBUG 717 printf(" found APM BIOS version %04x\n", apm_version); 718 printf("apm%d: Code32 0x%08x, Code16 0x%08x, Data 0x%08x\n", 719 unit, sc->cs32_base, sc->cs16_base, sc->ds_base); 720 printf("apm%d: Code entry 0x%08x, Idling CPU %s, Management %s\n", 721 unit, sc->cs_entry, is_enabled(sc->idle_cpu), 722 is_enabled(!sc->disabled)); 723 printf("apm%d: CS_limit=%x, DS_limit=%x\n", 724 unit, sc->cs_limit, sc->ds_limit); 725#endif /* APM_DEBUG */ 726 727 sc->cs_limit = 0xffff; 728 sc->ds_limit = 0xffff; 729 730 /* setup GDT */ 731 setup_apm_gdt(sc->cs32_base, sc->cs16_base, sc->ds_base, 732 sc->cs_limit, sc->ds_limit); 733 734 /* setup entry point 48bit pointer */ 735 apm_addr.segment = GSEL(GAPMCODE32_SEL, SEL_KPL); 736 apm_addr.offset = sc->cs_entry; 737 738 /* Try to kick bios into 1.1 mode */ 739 apm_driver_version(); 740 sc->minorversion = ((apm_version & 0x00f0) >> 4) * 10 + 741 ((apm_version & 0x000f) >> 0); 742 sc->majorversion = ((apm_version & 0xf000) >> 12) * 10 + 743 ((apm_version & 0x0f00) >> 8); 744 745 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion); 746 747 if (sc->intversion >= INTVERSION(1, 1)) { 748 printf("apm%d: Engaged control %s\n", 749 unit, is_enabled(!sc->disengaged)); 750 } 751 752 printf(" found APM BIOS version %d.%d\n", 753 sc->majorversion, sc->minorversion); 754 printf("apm%d: Idling CPU %s\n", unit, is_enabled(sc->idle_cpu)); 755 756 /* enable power management */ 757 if (sc->disabled) { 758 if (apm_enable_disable_pm(sc, 1)) { 759 printf("Warning: APM enable function failed! [%x]\n", 760 apm_errno); 761 } 762 } 763 764 /* engage power managment (APM 1.1 or later) */ 765 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) { 766 if (apm_engage_disengage_pm(sc, 1)) { 767 printf("Warning: APM engage function failed [%x]\n", 768 apm_errno); 769 } 770 } 771 772 /* default suspend hook */ 773 sc->sc_suspend.ah_fun = apm_default_suspend; 774 sc->sc_suspend.ah_arg = sc; 775 sc->sc_suspend.ah_name = "default suspend"; 776 sc->sc_suspend.ah_order = APM_MAX_ORDER; 777 778 /* default resume hook */ 779 sc->sc_resume.ah_fun = apm_default_resume; 780 sc->sc_resume.ah_arg = sc; 781 sc->sc_resume.ah_name = "default resume"; 782 sc->sc_resume.ah_order = APM_MIN_ORDER; 783 784 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend); 785 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume); 786 787 apm_event_enable(sc); 788 789 sc->initialized = 1; 790 791#ifdef DEVFS 792 sprintf(name,"apm%d",unit); 793 sc->sc_devfs_token = devfs_add_devsw( 794 "/", name, &apm_cdevsw, unit, DV_CHR, 0, 0, 0600); 795#endif 796 return 0; 797} 798 799static int 800apmopen(dev_t dev, int flag, int fmt, struct proc *p) 801{ 802 struct apm_softc *sc = &apm_softc[minor(dev)]; 803 804 if (minor(dev) >= NAPM) { 805 return (ENXIO); 806 } 807 if (!sc->initialized) { 808 return ENXIO; 809 } 810 return 0; 811} 812 813static int 814apmclose(dev_t dev, int flag, int fmt, struct proc *p) 815{ 816 return 0; 817} 818 819static int 820apmioctl(dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p) 821{ 822 struct apm_softc *sc = &apm_softc[minor(dev)]; 823 int error = 0; 824 825#ifdef APM_DEBUG 826 printf("APM ioctl: minor = %d, cmd = 0x%x\n", minor(dev), cmd); 827#endif 828 829 if (minor(dev) >= NAPM) { 830 return ENXIO; 831 } 832 if (!sc->initialized) { 833 return ENXIO; 834 } 835 switch (cmd) { 836 case APMIO_SUSPEND: 837 apm_suspend(); 838 break; 839 case APMIO_GETINFO: 840 if (apm_get_info(sc, (apm_info_t)addr)) { 841 error = ENXIO; 842 } 843 break; 844 case APMIO_ENABLE: 845 apm_event_enable(sc); 846 break; 847 case APMIO_DISABLE: 848 apm_event_disable(sc); 849 break; 850 case APMIO_HALTCPU: 851 apm_halt_cpu(sc); 852 break; 853 case APMIO_NOTHALTCPU: 854 apm_not_halt_cpu(sc); 855 break; 856 case APMIO_DISPLAYOFF: 857 if (apm_display_off()) { 858 error = ENXIO; 859 } 860 break; 861 default: 862 error = EINVAL; 863 break; 864 } 865 return error; 866} 867 868 869static apm_devsw_installed = 0; 870 871static void 872apm_drvinit(void *unused) 873{ 874 dev_t dev; 875 876 if( ! apm_devsw_installed ) { 877 dev = makedev(CDEV_MAJOR,0); 878 cdevsw_add(&dev,&apm_cdevsw,NULL); 879 apm_devsw_installed = 1; 880 } 881} 882 883SYSINIT(apmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,apm_drvinit,NULL) 884