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