apm.c revision 220839
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 19#include <sys/cdefs.h> 20__FBSDID("$FreeBSD: head/sys/i386/bios/apm.c 220839 2011-04-19 16:20:55Z jkim $"); 21 22#include <sys/param.h> 23#include <sys/systm.h> 24#include <sys/bus.h> 25#include <sys/conf.h> 26#include <sys/condvar.h> 27#include <sys/eventhandler.h> 28#include <sys/fcntl.h> 29#include <sys/kernel.h> 30#include <sys/kthread.h> 31#include <sys/lock.h> 32#include <sys/module.h> 33#include <sys/mutex.h> 34#include <sys/poll.h> 35#include <sys/power.h> 36#include <sys/reboot.h> 37#include <sys/selinfo.h> 38#include <sys/signalvar.h> 39#include <sys/sysctl.h> 40#include <sys/syslog.h> 41#include <sys/time.h> 42#include <sys/uio.h> 43 44#include <machine/apm_bios.h> 45#include <machine/clock.h> 46#include <machine/endian.h> 47#include <machine/pc/bios.h> 48#include <machine/cpufunc.h> 49#include <machine/segments.h> 50#include <machine/stdarg.h> 51#include <machine/vm86.h> 52 53#include <machine/bus.h> 54#include <machine/resource.h> 55#include <sys/rman.h> 56 57#include <vm/vm.h> 58#include <vm/pmap.h> 59#include <vm/vm_param.h> 60 61#include <i386/bios/apm.h> 62#include <isa/rtc.h> 63 64/* Used by the apm_saver screen saver module */ 65int apm_display(int newstate); 66struct apm_softc apm_softc; 67 68static void apm_resume(void); 69static int apm_bioscall(void); 70static int apm_check_function_supported(u_int version, u_int func); 71 72static int apm_pm_func(u_long, void*, ...); 73 74static u_long apm_version; 75 76int apm_evindex; 77 78#define SCFLAG_ONORMAL 0x0000001 79#define SCFLAG_OCTL 0x0000002 80#define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL) 81 82#define APMDEV_NORMAL 0 83#define APMDEV_CTL 1 84 85#ifdef PC98 86extern int bios32_apm98(struct bios_regs *, u_int, u_short); 87 88/* PC98's SMM definition */ 89#define APM_NECSMM_PORT 0x6b8e 90#define APM_NECSMM_PORTSZ 1 91#define APM_NECSMM_EN 0x10 92static __inline void apm_enable_smm(struct apm_softc *); 93static __inline void apm_disable_smm(struct apm_softc *); 94int apm_necsmm_addr; 95u_int32_t apm_necsmm_mask; 96#endif 97 98static struct apmhook *hook[NAPM_HOOK]; /* XXX */ 99 100#define is_enabled(foo) ((foo) ? "enabled" : "disabled") 101 102/* Map version number to integer (keeps ordering of version numbers) */ 103#define INTVERSION(major, minor) ((major)*100 + (minor)) 104 105static d_open_t apmopen; 106static d_close_t apmclose; 107static d_write_t apmwrite; 108static d_ioctl_t apmioctl; 109static d_poll_t apmpoll; 110 111static struct cdevsw apm_cdevsw = { 112 .d_version = D_VERSION, 113 .d_flags = D_NEEDGIANT, 114 .d_open = apmopen, 115 .d_close = apmclose, 116 .d_write = apmwrite, 117 .d_ioctl = apmioctl, 118 .d_poll = apmpoll, 119 .d_name = "apm", 120}; 121 122static int apm_suspend_delay = 1; 123static int apm_standby_delay = 1; 124static int apm_swab_batt_minutes = 0; 125static int apm_debug = 0; 126 127#define APM_DPRINT(args...) do { \ 128 if (apm_debug) { \ 129 printf(args); \ 130 } \ 131} while (0) 132 133SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, ""); 134SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, ""); 135SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, ""); 136 137TUNABLE_INT("machdep.apm_swab_batt_minutes", &apm_swab_batt_minutes); 138SYSCTL_INT(_machdep, OID_AUTO, apm_swab_batt_minutes, CTLFLAG_RW, 139 &apm_swab_batt_minutes, 0, "Byte swap battery time value."); 140 141#ifdef PC98 142static __inline void 143apm_enable_smm(sc) 144 struct apm_softc *sc; 145{ 146 bus_space_tag_t iot = sc->sc_iot; 147 bus_space_handle_t ioh = sc->sc_ioh; 148 if (apm_necsmm_addr != 0) 149 bus_space_write_1(iot, ioh, 0, 150 (bus_space_read_1(iot, ioh, 0) | ~apm_necsmm_mask)); 151} 152 153static __inline void 154apm_disable_smm(sc) 155 struct apm_softc *sc; 156{ 157 bus_space_tag_t iot = sc->sc_iot; 158 bus_space_handle_t ioh = sc->sc_ioh; 159 if (apm_necsmm_addr != 0) 160 bus_space_write_1(iot, ioh, 0, 161 (bus_space_read_1(iot, ioh, 0) & apm_necsmm_mask)); 162} 163#endif 164 165/* 166 * return 0 if the function successfull, 167 * return 1 if the function unsuccessfull, 168 * return -1 if the function unsupported. 169 */ 170static int 171apm_bioscall(void) 172{ 173 struct apm_softc *sc = &apm_softc; 174 int errno = 0; 175 u_int apm_func = sc->bios.r.eax & 0xff; 176 177 if (!apm_check_function_supported(sc->intversion, apm_func)) { 178 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n", 179 apm_func, sc->majorversion, sc->minorversion); 180 return (-1); 181 } 182 183 sc->bios_busy = 1; 184#ifdef PC98 185 set_bios_selectors(&sc->bios.seg, BIOSCODE_FLAG | BIOSDATA_FLAG); 186 if (bios32_apm98(&sc->bios.r, sc->bios.entry, 187 GSEL(GBIOSCODE32_SEL, SEL_KPL)) != 0) 188 return 1; 189#else 190 if (sc->connectmode == APM_PROT32CONNECT) { 191 set_bios_selectors(&sc->bios.seg, 192 BIOSCODE_FLAG | BIOSDATA_FLAG); 193 errno = bios32(&sc->bios.r, 194 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL)); 195 } else { 196 errno = bios16(&sc->bios, NULL); 197 } 198#endif 199 sc->bios_busy = 0; 200 return (errno); 201} 202 203/* check whether APM function is supported (1) or not (0). */ 204static int 205apm_check_function_supported(u_int version, u_int func) 206{ 207 /* except driver version */ 208 if (func == APM_DRVVERSION) { 209 return (1); 210 } 211#ifdef PC98 212 if (func == APM_GETPWSTATUS) { 213 return (1); 214 } 215#endif 216 217 switch (version) { 218 case INTVERSION(1, 0): 219 if (func > APM_GETPMEVENT) { 220 return (0); /* not supported */ 221 } 222 break; 223 case INTVERSION(1, 1): 224 if (func > APM_ENGAGEDISENGAGEPM && 225 func < APM_OEMFUNC) { 226 return (0); /* not supported */ 227 } 228 break; 229 case INTVERSION(1, 2): 230 break; 231 } 232 233 return (1); /* supported */ 234} 235 236/* enable/disable power management */ 237static int 238apm_enable_disable_pm(int enable) 239{ 240 struct apm_softc *sc = &apm_softc; 241 242 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM; 243 244 if (sc->intversion >= INTVERSION(1, 1)) 245 sc->bios.r.ebx = PMDV_ALLDEV; 246 else 247 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */ 248 sc->bios.r.ecx = enable; 249 sc->bios.r.edx = 0; 250 return (apm_bioscall()); 251} 252 253/* register driver version (APM 1.1 or later) */ 254static int 255apm_driver_version(int version) 256{ 257 struct apm_softc *sc = &apm_softc; 258 259 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION; 260 sc->bios.r.ebx = 0x0; 261 sc->bios.r.ecx = version; 262 sc->bios.r.edx = 0; 263 264 if (apm_bioscall() == 0 && sc->bios.r.eax == version) 265 return (0); 266 267 /* Some old BIOSes don't return the connection version in %ax. */ 268 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION)) 269 return (0); 270 271 return (1); 272} 273 274/* engage/disengage power management (APM 1.1 or later) */ 275static int 276apm_engage_disengage_pm(int engage) 277{ 278 struct apm_softc *sc = &apm_softc; 279 280 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM; 281 sc->bios.r.ebx = PMDV_ALLDEV; 282 sc->bios.r.ecx = engage; 283 sc->bios.r.edx = 0; 284 return (apm_bioscall()); 285} 286 287/* get PM event */ 288static u_int 289apm_getevent(void) 290{ 291 struct apm_softc *sc = &apm_softc; 292 293 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT; 294 295 sc->bios.r.ebx = 0; 296 sc->bios.r.ecx = 0; 297 sc->bios.r.edx = 0; 298 if (apm_bioscall()) 299 return (PMEV_NOEVENT); 300 return (sc->bios.r.ebx & 0xffff); 301} 302 303/* suspend entire system */ 304static int 305apm_suspend_system(int state) 306{ 307 struct apm_softc *sc = &apm_softc; 308 309 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; 310 sc->bios.r.ebx = PMDV_ALLDEV; 311 sc->bios.r.ecx = state; 312 sc->bios.r.edx = 0; 313 314#ifdef PC98 315 apm_disable_smm(sc); 316#endif 317 if (apm_bioscall()) { 318 printf("Entire system suspend failure: errcode = %d\n", 319 0xff & (sc->bios.r.eax >> 8)); 320 return 1; 321 } 322#ifdef PC98 323 apm_enable_smm(sc); 324#endif 325 return 0; 326} 327 328/* Display control */ 329/* 330 * Experimental implementation: My laptop machine can't handle this function 331 * If your laptop can control the display via APM, please inform me. 332 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org> 333 */ 334int 335apm_display(int newstate) 336{ 337 struct apm_softc *sc = &apm_softc; 338 339 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; 340 sc->bios.r.ebx = PMDV_DISP0; 341 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND; 342 sc->bios.r.edx = 0; 343 if (apm_bioscall() == 0) { 344 return 0; 345 } 346 347 /* If failed, then try to blank all display devices instead. */ 348 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; 349 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */ 350 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND; 351 sc->bios.r.edx = 0; 352 if (apm_bioscall() == 0) { 353 return 0; 354 } 355 printf("Display off failure: errcode = %d\n", 356 0xff & (sc->bios.r.eax >> 8)); 357 return 1; 358} 359 360/* 361 * Turn off the entire system. 362 */ 363static void 364apm_power_off(void *junk, int howto) 365{ 366 struct apm_softc *sc = &apm_softc; 367 368 /* Not halting powering off, or not active */ 369 if (!(howto & RB_POWEROFF) || !apm_softc.active) 370 return; 371 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; 372 sc->bios.r.ebx = PMDV_ALLDEV; 373 sc->bios.r.ecx = PMST_OFF; 374 sc->bios.r.edx = 0; 375 (void) apm_bioscall(); 376} 377 378/* APM Battery low handler */ 379static void 380apm_battery_low(void) 381{ 382 printf("\007\007 * * * BATTERY IS LOW * * * \007\007"); 383} 384 385/* APM hook manager */ 386static struct apmhook * 387apm_add_hook(struct apmhook **list, struct apmhook *ah) 388{ 389 int s; 390 struct apmhook *p, *prev; 391 392 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name); 393 394 s = splhigh(); 395 if (ah == NULL) 396 panic("illegal apm_hook!"); 397 prev = NULL; 398 for (p = *list; p != NULL; prev = p, p = p->ah_next) 399 if (p->ah_order > ah->ah_order) 400 break; 401 402 if (prev == NULL) { 403 ah->ah_next = *list; 404 *list = ah; 405 } else { 406 ah->ah_next = prev->ah_next; 407 prev->ah_next = ah; 408 } 409 splx(s); 410 return ah; 411} 412 413static void 414apm_del_hook(struct apmhook **list, struct apmhook *ah) 415{ 416 int s; 417 struct apmhook *p, *prev; 418 419 s = splhigh(); 420 prev = NULL; 421 for (p = *list; p != NULL; prev = p, p = p->ah_next) 422 if (p == ah) 423 goto deleteit; 424 panic("Tried to delete unregistered apm_hook."); 425 goto nosuchnode; 426deleteit: 427 if (prev != NULL) 428 prev->ah_next = p->ah_next; 429 else 430 *list = p->ah_next; 431nosuchnode: 432 splx(s); 433} 434 435 436/* APM driver calls some functions automatically */ 437static void 438apm_execute_hook(struct apmhook *list) 439{ 440 struct apmhook *p; 441 442 for (p = list; p != NULL; p = p->ah_next) { 443 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name); 444 if ((*(p->ah_fun))(p->ah_arg)) 445 printf("Warning: APM hook \"%s\" failed", p->ah_name); 446 } 447} 448 449 450/* establish an apm hook */ 451struct apmhook * 452apm_hook_establish(int apmh, struct apmhook *ah) 453{ 454 if (apmh < 0 || apmh >= NAPM_HOOK) 455 return NULL; 456 457 return apm_add_hook(&hook[apmh], ah); 458} 459 460/* disestablish an apm hook */ 461void 462apm_hook_disestablish(int apmh, struct apmhook *ah) 463{ 464 if (apmh < 0 || apmh >= NAPM_HOOK) 465 return; 466 467 apm_del_hook(&hook[apmh], ah); 468} 469 470static int apm_record_event(struct apm_softc *, u_int); 471static void apm_processevent(void); 472 473static u_int apm_op_inprog = 0; 474 475static void 476apm_do_suspend(void) 477{ 478 struct apm_softc *sc = &apm_softc; 479 int error; 480 481 if (sc == NULL || sc->initialized == 0) 482 return; 483 484 apm_op_inprog = 0; 485 sc->suspends = sc->suspend_countdown = 0; 486 487 EVENTHANDLER_INVOKE(power_suspend); 488 489 /* 490 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since 491 * non-MPSAFE drivers need this. 492 */ 493 mtx_lock(&Giant); 494 error = DEVICE_SUSPEND(root_bus); 495 if (error) 496 goto backout; 497 498 apm_execute_hook(hook[APM_HOOK_SUSPEND]); 499 if (apm_suspend_system(PMST_SUSPEND) == 0) { 500 sc->suspending = 1; 501 apm_processevent(); 502 } else { 503 /* Failure, 'resume' the system again */ 504 apm_execute_hook(hook[APM_HOOK_RESUME]); 505 DEVICE_RESUME(root_bus); 506 } 507backout: 508 mtx_unlock(&Giant); 509 EVENTHANDLER_INVOKE(power_resume); 510} 511 512static void 513apm_do_standby(void) 514{ 515 struct apm_softc *sc = &apm_softc; 516 517 if (sc == NULL || sc->initialized == 0) 518 return; 519 520 apm_op_inprog = 0; 521 sc->standbys = sc->standby_countdown = 0; 522 523 /* 524 * As far as standby, we don't need to execute 525 * all of suspend hooks. 526 */ 527 if (apm_suspend_system(PMST_STANDBY) == 0) 528 apm_processevent(); 529 return; 530} 531 532static void 533apm_lastreq_notify(void) 534{ 535 struct apm_softc *sc = &apm_softc; 536 537 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; 538 sc->bios.r.ebx = PMDV_ALLDEV; 539 sc->bios.r.ecx = PMST_LASTREQNOTIFY; 540 sc->bios.r.edx = 0; 541 apm_bioscall(); 542} 543 544static int 545apm_lastreq_rejected(void) 546{ 547 struct apm_softc *sc = &apm_softc; 548 549 if (apm_op_inprog == 0) { 550 return 1; /* no operation in progress */ 551 } 552 553 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; 554 sc->bios.r.ebx = PMDV_ALLDEV; 555 sc->bios.r.ecx = PMST_LASTREQREJECT; 556 sc->bios.r.edx = 0; 557 558 if (apm_bioscall()) { 559 APM_DPRINT("apm_lastreq_rejected: failed\n"); 560 return 1; 561 } 562 apm_op_inprog = 0; 563 return 0; 564} 565 566/* 567 * Public interface to the suspend/resume: 568 * 569 * Execute suspend and resume hook before and after sleep, respectively. 570 * 571 */ 572 573void 574apm_suspend(int state) 575{ 576 struct apm_softc *sc = &apm_softc; 577 578 if (sc == NULL || sc->initialized == 0) 579 return; 580 581 switch (state) { 582 case PMST_SUSPEND: 583 if (sc->suspends) 584 return; 585 sc->suspends++; 586 sc->suspend_countdown = apm_suspend_delay; 587 break; 588 case PMST_STANDBY: 589 if (sc->standbys) 590 return; 591 sc->standbys++; 592 sc->standby_countdown = apm_standby_delay; 593 break; 594 default: 595 printf("apm_suspend: Unknown Suspend state 0x%x\n", state); 596 return; 597 } 598 599 apm_op_inprog++; 600 apm_lastreq_notify(); 601} 602 603static void 604apm_resume(void) 605{ 606 struct apm_softc *sc = &apm_softc; 607 608 if (sc == NULL || sc->initialized == 0 || sc->suspending == 0) 609 return; 610 611 sc->suspending = 0; 612 apm_execute_hook(hook[APM_HOOK_RESUME]); 613 mtx_lock(&Giant); 614 DEVICE_RESUME(root_bus); 615 mtx_unlock(&Giant); 616 EVENTHANDLER_INVOKE(power_resume); 617} 618 619 620/* get power status per battery */ 621static int 622apm_get_pwstatus(apm_pwstatus_t app) 623{ 624 struct apm_softc *sc = &apm_softc; 625 626 if (app->ap_device != PMDV_ALLDEV && 627 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL)) 628 return 1; 629 630 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS; 631 sc->bios.r.ebx = app->ap_device; 632 sc->bios.r.ecx = 0; 633 sc->bios.r.edx = 0xffff; /* default to unknown battery time */ 634 635 if (apm_bioscall()) 636 return 1; 637 638 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff; 639 app->ap_batt_stat = sc->bios.r.ebx & 0xff; 640 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff; 641 app->ap_batt_life = sc->bios.r.ecx & 0xff; 642 sc->bios.r.edx &= 0xffff; 643 if (apm_swab_batt_minutes) 644 sc->bios.r.edx = __bswap16(sc->bios.r.edx) | 0x8000; 645 if (sc->bios.r.edx == 0xffff) /* Time is unknown */ 646 app->ap_batt_time = -1; 647 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */ 648 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60; 649 else /* Time is in seconds */ 650 app->ap_batt_time = sc->bios.r.edx; 651 652 return 0; 653} 654 655 656/* get APM information */ 657static int 658apm_get_info(apm_info_t aip) 659{ 660 struct apm_softc *sc = &apm_softc; 661 struct apm_pwstatus aps; 662 663 bzero(&aps, sizeof(aps)); 664 aps.ap_device = PMDV_ALLDEV; 665 if (apm_get_pwstatus(&aps)) 666 return 1; 667 668 aip->ai_infoversion = 1; 669 aip->ai_acline = aps.ap_acline; 670 aip->ai_batt_stat = aps.ap_batt_stat; 671 aip->ai_batt_life = aps.ap_batt_life; 672 aip->ai_batt_time = aps.ap_batt_time; 673 aip->ai_major = (u_int)sc->majorversion; 674 aip->ai_minor = (u_int)sc->minorversion; 675 aip->ai_status = (u_int)sc->active; 676 677 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES; 678 sc->bios.r.ebx = 0; 679 sc->bios.r.ecx = 0; 680 sc->bios.r.edx = 0; 681 if (apm_bioscall()) { 682 aip->ai_batteries = 0xffffffff; /* Unknown */ 683 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */ 684 } else { 685 aip->ai_batteries = sc->bios.r.ebx & 0xff; 686 aip->ai_capabilities = sc->bios.r.ecx & 0xff; 687 } 688 689 bzero(aip->ai_spare, sizeof aip->ai_spare); 690 691 return 0; 692} 693 694 695/* inform APM BIOS that CPU is idle */ 696void 697apm_cpu_idle(void) 698{ 699 struct apm_softc *sc = &apm_softc; 700 701 if (sc->active) { 702 703 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE; 704 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0; 705 (void) apm_bioscall(); 706 } 707 /* 708 * Some APM implementation halts CPU in BIOS, whenever 709 * "CPU-idle" function are invoked, but swtch() of 710 * FreeBSD halts CPU, therefore, CPU is halted twice 711 * in the sched loop. It makes the interrupt latency 712 * terribly long and be able to cause a serious problem 713 * in interrupt processing. We prevent it by removing 714 * "hlt" operation from swtch() and managed it under 715 * APM driver. 716 */ 717 if (!sc->active || sc->always_halt_cpu) 718 halt(); /* wait for interrupt */ 719} 720 721/* inform APM BIOS that CPU is busy */ 722void 723apm_cpu_busy(void) 724{ 725 struct apm_softc *sc = &apm_softc; 726 727 /* 728 * The APM specification says this is only necessary if your BIOS 729 * slows down the processor in the idle task, otherwise it's not 730 * necessary. 731 */ 732 if (sc->slow_idle_cpu && sc->active) { 733 734 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY; 735 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0; 736 apm_bioscall(); 737 } 738} 739 740 741/* 742 * APM thread loop. 743 * 744 * This routine wakes up from time to time to deal with delaying the 745 * suspend of the system, or other events. 746 */ 747static void 748apm_event_thread(void *arg) 749{ 750 struct apm_softc *sc = &apm_softc; 751 752 sc->running = 1; 753 while (sc->active) { 754 if (apm_op_inprog) 755 apm_lastreq_notify(); 756 if (sc->standbys && sc->standby_countdown-- <= 0) 757 apm_do_standby(); 758 if (sc->suspends && sc->suspend_countdown-- <= 0) 759 apm_do_suspend(); 760 if (!sc->bios_busy) 761 apm_processevent(); 762 mtx_lock(&sc->mtx); 763 cv_timedwait(&sc->cv, &sc->mtx, 10 * hz / 9); 764 mtx_unlock(&sc->mtx); 765 } 766 sc->running = 0; 767 kproc_exit(0); 768} 769 770/* enable APM BIOS */ 771static void 772apm_event_enable(void) 773{ 774 struct apm_softc *sc = &apm_softc; 775 776 APM_DPRINT("called apm_event_enable()\n"); 777 778 if (sc == NULL || sc->initialized == 0) 779 return; 780 781 /* Start the thread */ 782 sc->active = 1; 783 if (kproc_create(apm_event_thread, sc, &sc->event_thread, 0, 0, 784 "apm worker")) 785 panic("Cannot create apm worker thread"); 786 787 return; 788} 789 790/* disable APM BIOS */ 791static void 792apm_event_disable(void) 793{ 794 struct apm_softc *sc = &apm_softc; 795 796 APM_DPRINT("called apm_event_disable()\n"); 797 798 if (sc == NULL || sc->initialized == 0) 799 return; 800 801 mtx_lock(&sc->mtx); 802 sc->active = 0; 803 while (sc->running) { 804 cv_broadcast(&sc->cv); 805 msleep(sc->event_thread, &sc->mtx, PWAIT, "apmdie", 0); 806 } 807 mtx_unlock(&sc->mtx); 808 sc->event_thread = NULL; 809 return; 810} 811 812/* halt CPU in scheduling loop */ 813static void 814apm_halt_cpu(void) 815{ 816 struct apm_softc *sc = &apm_softc; 817 818 if (sc == NULL || sc->initialized == 0) 819 return; 820 821 sc->always_halt_cpu = 1; 822 823 return; 824} 825 826/* don't halt CPU in scheduling loop */ 827static void 828apm_not_halt_cpu(void) 829{ 830 struct apm_softc *sc = &apm_softc; 831 832 if (sc == NULL || sc->initialized == 0) 833 return; 834 835 sc->always_halt_cpu = 0; 836 837 return; 838} 839 840/* device driver definitions */ 841 842/* 843 * Module event 844 */ 845 846static int 847apm_modevent(struct module *mod, int event, void *junk) 848{ 849 850 switch (event) { 851 case MOD_LOAD: 852 if (!cold) 853 return (EPERM); 854 break; 855 case MOD_UNLOAD: 856 if (!cold && power_pm_get_type() == POWER_PM_TYPE_APM) 857 return (EBUSY); 858 break; 859 default: 860 break; 861 } 862 863 return (0); 864} 865 866/* 867 * Create "connection point" 868 */ 869static void 870apm_identify(driver_t *driver, device_t parent) 871{ 872 device_t child; 873 874 if (!cold) { 875 printf("Don't load this driver from userland!!\n"); 876 return; 877 } 878 879 if (resource_disabled("apm", 0)) 880 return; 881 882 child = BUS_ADD_CHILD(parent, 0, "apm", 0); 883 if (child == NULL) 884 panic("apm_identify"); 885} 886 887/* 888 * probe for APM BIOS 889 */ 890static int 891apm_probe(device_t dev) 892{ 893#define APM_KERNBASE KERNBASE 894 struct vm86frame vmf; 895 struct apm_softc *sc = &apm_softc; 896#ifdef PC98 897 int rid; 898#endif 899 900 device_set_desc(dev, "APM BIOS"); 901 if (device_get_unit(dev) > 0) { 902 printf("apm: Only one APM driver supported.\n"); 903 return ENXIO; 904 } 905 906 if (power_pm_get_type() != POWER_PM_TYPE_NONE && 907 power_pm_get_type() != POWER_PM_TYPE_APM) { 908 printf("apm: Other PM system enabled.\n"); 909 return ENXIO; 910 } 911 912 bzero(&vmf, sizeof(struct vm86frame)); /* safety */ 913 bzero(&apm_softc, sizeof(apm_softc)); 914 vmf.vmf_ah = APM_BIOS; 915 vmf.vmf_al = APM_INSTCHECK; 916 vmf.vmf_bx = 0; 917 if (vm86_intcall(APM_INT, &vmf)) 918 return ENXIO; /* APM not found */ 919 if (vmf.vmf_bx != 0x504d) { 920 printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx); 921 return ENXIO; 922 } 923 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) { 924 printf("apm: protected mode connections are not supported\n"); 925 return ENXIO; 926 } 927 928 apm_version = vmf.vmf_ax; 929 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0); 930 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0); 931 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0); 932 933 vmf.vmf_ah = APM_BIOS; 934 vmf.vmf_al = APM_DISCONNECT; 935 vmf.vmf_bx = 0; 936 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */ 937 938#ifdef PC98 939 /* PC98 have bogos APM 32bit BIOS */ 940 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) == 0) 941 return ENXIO; 942 rid = 0; 943 bus_set_resource(dev, SYS_RES_IOPORT, rid, 944 APM_NECSMM_PORT, APM_NECSMM_PORTSZ); 945 sc->sc_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 946 APM_NECSMM_PORT, ~0, APM_NECSMM_PORTSZ, RF_ACTIVE); 947 if (sc->sc_res == NULL) { 948 printf("apm: cannot open NEC smm device\n"); 949 return ENXIO; 950 } 951 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_res); 952 953 vmf.vmf_ah = APM_BIOS; 954 vmf.vmf_al = APM_PROT32CONNECT; 955 vmf.vmf_bx = 0; 956 if (vm86_intcall(APM_INT, &vmf)) { 957 printf("apm: 32-bit connection error.\n"); 958 return (ENXIO); 959 } 960 961 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE; 962 sc->bios.seg.code32.limit = 0xffff; 963 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE; 964 sc->bios.seg.code16.limit = 0xffff; 965 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE; 966 sc->bios.seg.data.limit = 0xffff; 967 sc->bios.entry = vmf.vmf_ebx; 968 sc->connectmode = APM_PROT32CONNECT; 969#else 970 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) { 971 vmf.vmf_ah = APM_BIOS; 972 vmf.vmf_al = APM_PROT32CONNECT; 973 vmf.vmf_bx = 0; 974 if (vm86_intcall(APM_INT, &vmf)) { 975 printf("apm: 32-bit connection error.\n"); 976 return (ENXIO); 977 } 978 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE; 979 sc->bios.seg.code32.limit = 0xffff; 980 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE; 981 sc->bios.seg.code16.limit = 0xffff; 982 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE; 983 sc->bios.seg.data.limit = 0xffff; 984 sc->bios.entry = vmf.vmf_ebx; 985 sc->connectmode = APM_PROT32CONNECT; 986 } else { 987 /* use 16-bit connection */ 988 vmf.vmf_ah = APM_BIOS; 989 vmf.vmf_al = APM_PROT16CONNECT; 990 vmf.vmf_bx = 0; 991 if (vm86_intcall(APM_INT, &vmf)) { 992 printf("apm: 16-bit connection error.\n"); 993 return (ENXIO); 994 } 995 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE; 996 sc->bios.seg.code16.limit = 0xffff; 997 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE; 998 sc->bios.seg.data.limit = 0xffff; 999 sc->bios.entry = vmf.vmf_bx; 1000 sc->connectmode = APM_PROT16CONNECT; 1001 } 1002#endif 1003 return(0); 1004} 1005 1006 1007/* 1008 * return 0 if the user will notice and handle the event, 1009 * return 1 if the kernel driver should do so. 1010 */ 1011static int 1012apm_record_event(struct apm_softc *sc, u_int event_type) 1013{ 1014 struct apm_event_info *evp; 1015 1016 if ((sc->sc_flags & SCFLAG_OPEN) == 0) 1017 return 1; /* no user waiting */ 1018 if (sc->event_count == APM_NEVENTS) 1019 return 1; /* overflow */ 1020 if (sc->event_filter[event_type] == 0) 1021 return 1; /* not registered */ 1022 evp = &sc->event_list[sc->event_ptr]; 1023 sc->event_count++; 1024 sc->event_ptr++; 1025 sc->event_ptr %= APM_NEVENTS; 1026 evp->type = event_type; 1027 evp->index = ++apm_evindex; 1028 selwakeuppri(&sc->sc_rsel, PZERO); 1029 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */ 1030} 1031 1032/* Power profile */ 1033static void 1034apm_power_profile(struct apm_softc *sc) 1035{ 1036 int state; 1037 struct apm_info info; 1038 static int apm_acline = 0; 1039 1040 if (apm_get_info(&info)) 1041 return; 1042 1043 if (apm_acline != info.ai_acline) { 1044 apm_acline = info.ai_acline; 1045 state = apm_acline ? POWER_PROFILE_PERFORMANCE : POWER_PROFILE_ECONOMY; 1046 power_profile_set_state(state); 1047 } 1048} 1049 1050/* Process APM event */ 1051static void 1052apm_processevent(void) 1053{ 1054 int apm_event; 1055 struct apm_softc *sc = &apm_softc; 1056 1057#define OPMEV_DEBUGMESSAGE(symbol) case symbol: \ 1058 APM_DPRINT("Received APM Event: " #symbol "\n"); 1059 1060 do { 1061 apm_event = apm_getevent(); 1062 switch (apm_event) { 1063 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ); 1064 if (apm_op_inprog == 0) { 1065 apm_op_inprog++; 1066 if (apm_record_event(sc, apm_event)) { 1067 apm_suspend(PMST_STANDBY); 1068 } 1069 } 1070 break; 1071 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ); 1072 if (apm_op_inprog == 0) { 1073 apm_op_inprog++; 1074 if (apm_record_event(sc, apm_event)) { 1075 apm_suspend(PMST_STANDBY); 1076 } 1077 } 1078 break; 1079 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ); 1080 apm_lastreq_notify(); 1081 if (apm_op_inprog == 0) { 1082 apm_op_inprog++; 1083 if (apm_record_event(sc, apm_event)) { 1084 apm_do_suspend(); 1085 } 1086 } 1087 return; /* XXX skip the rest */ 1088 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ); 1089 apm_lastreq_notify(); 1090 if (apm_op_inprog == 0) { 1091 apm_op_inprog++; 1092 if (apm_record_event(sc, apm_event)) { 1093 apm_do_suspend(); 1094 } 1095 } 1096 return; /* XXX skip the rest */ 1097 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND); 1098 apm_do_suspend(); 1099 break; 1100 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME); 1101 apm_record_event(sc, apm_event); 1102 apm_resume(); 1103 break; 1104 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME); 1105 apm_record_event(sc, apm_event); 1106 apm_resume(); 1107 break; 1108 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME); 1109 apm_record_event(sc, apm_event); 1110 break; 1111 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW); 1112 if (apm_record_event(sc, apm_event)) { 1113 apm_battery_low(); 1114 apm_suspend(PMST_SUSPEND); 1115 } 1116 break; 1117 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE); 1118 apm_record_event(sc, apm_event); 1119 apm_power_profile(sc); 1120 break; 1121 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME); 1122 apm_record_event(sc, apm_event); 1123 inittodr(0); /* adjust time to RTC */ 1124 break; 1125 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE); 1126 apm_record_event(sc, apm_event); 1127 apm_power_profile(sc); 1128 break; 1129 case PMEV_NOEVENT: 1130 break; 1131 default: 1132 printf("Unknown Original APM Event 0x%x\n", apm_event); 1133 break; 1134 } 1135 } while (apm_event != PMEV_NOEVENT); 1136#ifdef PC98 1137 apm_disable_smm(sc); 1138#endif 1139} 1140 1141/* 1142 * Attach APM: 1143 * 1144 * Initialize APM driver 1145 */ 1146 1147static int 1148apm_attach(device_t dev) 1149{ 1150 struct apm_softc *sc = &apm_softc; 1151 int drv_version; 1152#ifdef PC98 1153 int rid; 1154#endif 1155 mtx_init(&sc->mtx, device_get_nameunit(dev), "apm", MTX_DEF); 1156 cv_init(&sc->cv, "cbb cv"); 1157 1158#ifndef PC98 1159 if (device_get_flags(dev) & 0x20) 1160 atrtcclock_disable = 1; 1161#endif 1162 1163 sc->initialized = 0; 1164 1165 /* Must be externally enabled */ 1166 sc->active = 0; 1167 1168 /* Always call HLT in idle loop */ 1169 sc->always_halt_cpu = 1; 1170 1171 getenv_int("debug.apm_debug", &apm_debug); 1172 1173 /* print bootstrap messages */ 1174 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version); 1175 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n", 1176 sc->bios.seg.code16.base, sc->bios.seg.data.base); 1177 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n", 1178 sc->bios.entry, is_enabled(sc->slow_idle_cpu), 1179 is_enabled(!sc->disabled)); 1180 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n", 1181 sc->bios.seg.code16.limit, sc->bios.seg.data.limit); 1182 1183#ifdef PC98 1184 rid = 0; 1185 sc->sc_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 1186 APM_NECSMM_PORT, ~0, APM_NECSMM_PORTSZ, RF_ACTIVE); 1187 if (sc->sc_res == NULL) 1188 panic("%s: counldn't map I/O ports", device_get_name(dev)); 1189 sc->sc_iot = rman_get_bustag(sc->sc_res); 1190 sc->sc_ioh = rman_get_bushandle(sc->sc_res); 1191 1192 if (apm_version==0x112 || apm_version==0x111 || apm_version==0x110) 1193 apm_necsmm_addr = APM_NECSMM_PORT; 1194 else 1195 apm_necsmm_addr = 0; 1196 apm_necsmm_mask = ~APM_NECSMM_EN; 1197#endif /* PC98 */ 1198 1199 /* 1200 * In one test, apm bios version was 1.02; an attempt to register 1201 * a 1.04 driver resulted in a 1.00 connection! Registering a 1202 * 1.02 driver resulted in a 1.02 connection. 1203 */ 1204 drv_version = apm_version > 0x102 ? 0x102 : apm_version; 1205 for (; drv_version > 0x100; drv_version--) 1206 if (apm_driver_version(drv_version) == 0) 1207 break; 1208 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 + 1209 ((drv_version & 0x000f) >> 0); 1210 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 + 1211 ((apm_version & 0x0f00) >> 8); 1212 1213 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion); 1214 1215 if (sc->intversion >= INTVERSION(1, 1)) 1216 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged)); 1217 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n", 1218 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8), 1219 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0), 1220 sc->majorversion, sc->minorversion); 1221 1222 1223 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu)); 1224 /* enable power management */ 1225 if (sc->disabled) { 1226 if (apm_enable_disable_pm(1)) { 1227 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n", 1228 (sc->bios.r.eax >> 8) & 0xff); 1229 } 1230 } 1231 1232 /* engage power managment (APM 1.1 or later) */ 1233 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) { 1234 if (apm_engage_disengage_pm(1)) { 1235 APM_DPRINT("apm: *Warning* engage function failed err=[%x]", 1236 (sc->bios.r.eax >> 8) & 0xff); 1237 APM_DPRINT(" (Docked or using external power?).\n"); 1238 } 1239 } 1240 1241 /* Power the system off using APM */ 1242 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL, 1243 SHUTDOWN_PRI_LAST); 1244 1245 /* Register APM again to pass the correct argument of pm_func. */ 1246 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, sc); 1247 1248 sc->initialized = 1; 1249 sc->suspending = 0; 1250 sc->running = 0; 1251 1252 make_dev(&apm_cdevsw, APMDEV_NORMAL, 1253 UID_ROOT, GID_OPERATOR, 0664, "apm"); 1254 make_dev(&apm_cdevsw, APMDEV_CTL, 1255 UID_ROOT, GID_OPERATOR, 0660, "apmctl"); 1256 return 0; 1257} 1258 1259static int 1260apmopen(struct cdev *dev, int flag, int fmt, struct thread *td) 1261{ 1262 struct apm_softc *sc = &apm_softc; 1263 1264 if (sc == NULL || sc->initialized == 0) 1265 return (ENXIO); 1266 1267 switch (dev2unit(dev)) { 1268 case APMDEV_CTL: 1269 if (!(flag & FWRITE)) 1270 return EINVAL; 1271 if (sc->sc_flags & SCFLAG_OCTL) 1272 return EBUSY; 1273 sc->sc_flags |= SCFLAG_OCTL; 1274 bzero(sc->event_filter, sizeof sc->event_filter); 1275 break; 1276 case APMDEV_NORMAL: 1277 sc->sc_flags |= SCFLAG_ONORMAL; 1278 break; 1279 } 1280 return 0; 1281} 1282 1283static int 1284apmclose(struct cdev *dev, int flag, int fmt, struct thread *td) 1285{ 1286 struct apm_softc *sc = &apm_softc; 1287 1288 switch (dev2unit(dev)) { 1289 case APMDEV_CTL: 1290 apm_lastreq_rejected(); 1291 sc->sc_flags &= ~SCFLAG_OCTL; 1292 bzero(sc->event_filter, sizeof sc->event_filter); 1293 break; 1294 case APMDEV_NORMAL: 1295 sc->sc_flags &= ~SCFLAG_ONORMAL; 1296 break; 1297 } 1298 if ((sc->sc_flags & SCFLAG_OPEN) == 0) { 1299 sc->event_count = 0; 1300 sc->event_ptr = 0; 1301 } 1302 return 0; 1303} 1304 1305static int 1306apmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 1307{ 1308 struct apm_softc *sc = &apm_softc; 1309 struct apm_bios_arg *args; 1310 int error = 0; 1311 int ret; 1312 int newstate; 1313 1314 if (sc == NULL || sc->initialized == 0) 1315 return (ENXIO); 1316 1317 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd); 1318 switch (cmd) { 1319 case APMIO_SUSPEND: 1320 if (!(flag & FWRITE)) 1321 return (EPERM); 1322 if (sc->active) 1323 apm_suspend(PMST_SUSPEND); 1324 else 1325 error = EINVAL; 1326 break; 1327 1328 case APMIO_STANDBY: 1329 if (!(flag & FWRITE)) 1330 return (EPERM); 1331 if (sc->active) 1332 apm_suspend(PMST_STANDBY); 1333 else 1334 error = EINVAL; 1335 break; 1336 1337 case APMIO_GETINFO_OLD: 1338 { 1339 struct apm_info info; 1340 apm_info_old_t aiop; 1341 1342 if (apm_get_info(&info)) 1343 error = ENXIO; 1344 aiop = (apm_info_old_t)addr; 1345 aiop->ai_major = info.ai_major; 1346 aiop->ai_minor = info.ai_minor; 1347 aiop->ai_acline = info.ai_acline; 1348 aiop->ai_batt_stat = info.ai_batt_stat; 1349 aiop->ai_batt_life = info.ai_batt_life; 1350 aiop->ai_status = info.ai_status; 1351 } 1352 break; 1353 case APMIO_GETINFO: 1354 if (apm_get_info((apm_info_t)addr)) 1355 error = ENXIO; 1356 break; 1357 case APMIO_GETPWSTATUS: 1358 if (apm_get_pwstatus((apm_pwstatus_t)addr)) 1359 error = ENXIO; 1360 break; 1361 case APMIO_ENABLE: 1362 if (!(flag & FWRITE)) 1363 return (EPERM); 1364 apm_event_enable(); 1365 break; 1366 case APMIO_DISABLE: 1367 if (!(flag & FWRITE)) 1368 return (EPERM); 1369 apm_event_disable(); 1370 break; 1371 case APMIO_HALTCPU: 1372 if (!(flag & FWRITE)) 1373 return (EPERM); 1374 apm_halt_cpu(); 1375 break; 1376 case APMIO_NOTHALTCPU: 1377 if (!(flag & FWRITE)) 1378 return (EPERM); 1379 apm_not_halt_cpu(); 1380 break; 1381 case APMIO_DISPLAY: 1382 if (!(flag & FWRITE)) 1383 return (EPERM); 1384 newstate = *(int *)addr; 1385 if (apm_display(newstate)) 1386 error = ENXIO; 1387 break; 1388 case APMIO_BIOS: 1389 if (!(flag & FWRITE)) 1390 return (EPERM); 1391 /* XXX compatibility with the old interface */ 1392 args = (struct apm_bios_arg *)addr; 1393#ifdef PC98 1394 if (((args->eax >> 8) & 0xff) == 0x53) { 1395 sc->bios.r.eax = args->eax & ~0xffff; 1396 sc->bios.r.eax |= APM_BIOS << 8; 1397 switch (args->eax & 0xff) { 1398 case 0x0a: 1399 sc->bios.r.eax |= APM_GETPWSTATUS; 1400 break; 1401 case 0x0e: 1402 sc->bios.r.eax |= APM_DRVVERSION; 1403 break; 1404 default: 1405 sc->bios.r.eax |= args->eax & 0xff; 1406 break; 1407 } 1408 } else 1409#endif 1410 sc->bios.r.eax = args->eax; 1411 sc->bios.r.ebx = args->ebx; 1412 sc->bios.r.ecx = args->ecx; 1413 sc->bios.r.edx = args->edx; 1414 sc->bios.r.esi = args->esi; 1415 sc->bios.r.edi = args->edi; 1416 if ((ret = apm_bioscall())) { 1417 /* 1418 * Return code 1 means bios call was unsuccessful. 1419 * Error code is stored in %ah. 1420 * Return code -1 means bios call was unsupported 1421 * in the APM BIOS version. 1422 */ 1423 if (ret == -1) { 1424 error = EINVAL; 1425 } 1426 } else { 1427 /* 1428 * Return code 0 means bios call was successful. 1429 * We need only %al and can discard %ah. 1430 */ 1431 sc->bios.r.eax &= 0xff; 1432 } 1433 args->eax = sc->bios.r.eax; 1434 args->ebx = sc->bios.r.ebx; 1435 args->ecx = sc->bios.r.ecx; 1436 args->edx = sc->bios.r.edx; 1437 args->esi = sc->bios.r.esi; 1438 args->edi = sc->bios.r.edi; 1439 break; 1440 default: 1441 error = EINVAL; 1442 break; 1443 } 1444 1445 /* for /dev/apmctl */ 1446 if (dev2unit(dev) == APMDEV_CTL) { 1447 struct apm_event_info *evp; 1448 int i; 1449 1450 error = 0; 1451 switch (cmd) { 1452 case APMIO_NEXTEVENT: 1453 if (!sc->event_count) { 1454 error = EAGAIN; 1455 } else { 1456 evp = (struct apm_event_info *)addr; 1457 i = sc->event_ptr + APM_NEVENTS - sc->event_count; 1458 i %= APM_NEVENTS; 1459 *evp = sc->event_list[i]; 1460 sc->event_count--; 1461 } 1462 break; 1463 case APMIO_REJECTLASTREQ: 1464 if (apm_lastreq_rejected()) { 1465 error = EINVAL; 1466 } 1467 break; 1468 default: 1469 error = EINVAL; 1470 break; 1471 } 1472 } 1473 1474 return error; 1475} 1476 1477static int 1478apmwrite(struct cdev *dev, struct uio *uio, int ioflag) 1479{ 1480 struct apm_softc *sc = &apm_softc; 1481 u_int event_type; 1482 int error; 1483 u_char enabled; 1484 1485 if (dev2unit(dev) != APMDEV_CTL) 1486 return(ENODEV); 1487 if (uio->uio_resid != sizeof(u_int)) 1488 return(E2BIG); 1489 1490 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio))) 1491 return(error); 1492 1493 if (event_type < 0 || event_type >= APM_NPMEV) 1494 return(EINVAL); 1495 1496 if (sc->event_filter[event_type] == 0) { 1497 enabled = 1; 1498 } else { 1499 enabled = 0; 1500 } 1501 sc->event_filter[event_type] = enabled; 1502 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled)); 1503 1504 return uio->uio_resid; 1505} 1506 1507static int 1508apmpoll(struct cdev *dev, int events, struct thread *td) 1509{ 1510 struct apm_softc *sc = &apm_softc; 1511 int revents = 0; 1512 1513 if (events & (POLLIN | POLLRDNORM)) { 1514 if (sc->event_count) { 1515 revents |= events & (POLLIN | POLLRDNORM); 1516 } else { 1517 selrecord(td, &sc->sc_rsel); 1518 } 1519 } 1520 1521 return (revents); 1522} 1523 1524static device_method_t apm_methods[] = { 1525 /* Device interface */ 1526 DEVMETHOD(device_identify, apm_identify), 1527 DEVMETHOD(device_probe, apm_probe), 1528 DEVMETHOD(device_attach, apm_attach), 1529 1530 { 0, 0 } 1531}; 1532 1533static driver_t apm_driver = { 1534 "apm", 1535 apm_methods, 1536 1, /* no softc (XXX) */ 1537}; 1538 1539static devclass_t apm_devclass; 1540 1541DRIVER_MODULE(apm, legacy, apm_driver, apm_devclass, apm_modevent, 0); 1542MODULE_VERSION(apm, 1); 1543 1544static int 1545apm_pm_func(u_long cmd, void *arg, ...) 1546{ 1547 int state, apm_state; 1548 int error; 1549 va_list ap; 1550 1551 error = 0; 1552 switch (cmd) { 1553 case POWER_CMD_SUSPEND: 1554 va_start(ap, arg); 1555 state = va_arg(ap, int); 1556 va_end(ap); 1557 1558 switch (state) { 1559 case POWER_SLEEP_STATE_STANDBY: 1560 apm_state = PMST_STANDBY; 1561 break; 1562 case POWER_SLEEP_STATE_SUSPEND: 1563 case POWER_SLEEP_STATE_HIBERNATE: 1564 apm_state = PMST_SUSPEND; 1565 break; 1566 default: 1567 error = EINVAL; 1568 goto out; 1569 } 1570 1571 apm_suspend(apm_state); 1572 break; 1573 1574 default: 1575 error = EINVAL; 1576 goto out; 1577 } 1578 1579out: 1580 return (error); 1581} 1582 1583static void 1584apm_pm_register(void *arg) 1585{ 1586 1587 if (!resource_disabled("apm", 0)) 1588 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, NULL); 1589} 1590 1591SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, apm_pm_register, 0); 1592