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