apm.c revision 173598
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 173598 2007-11-14 05:43:55Z julian $"); 21 22#include <sys/param.h> 23#include <sys/systm.h> 24#include <sys/bus.h> 25#include <sys/clock.h> 26#include <sys/conf.h> 27#include <sys/condvar.h> 28#include <sys/eventhandler.h> 29#include <sys/fcntl.h> 30#include <sys/kernel.h> 31#include <sys/kthread.h> 32#include <sys/lock.h> 33#include <sys/module.h> 34#include <sys/mutex.h> 35#include <sys/poll.h> 36#include <sys/power.h> 37#include <sys/reboot.h> 38#include <sys/selinfo.h> 39#include <sys/signalvar.h> 40#include <sys/sysctl.h> 41#include <sys/syslog.h> 42#include <sys/time.h> 43#include <sys/uio.h> 44 45#include <machine/apm_bios.h> 46#include <machine/clock.h> 47#include <machine/endian.h> 48#include <machine/pc/bios.h> 49#include <machine/cpufunc.h> 50#include <machine/segments.h> 51#include <machine/stdarg.h> 52#include <machine/vm86.h> 53 54#include <machine/bus.h> 55#include <machine/resource.h> 56#include <sys/rman.h> 57 58#include <vm/vm.h> 59#include <vm/pmap.h> 60#include <vm/vm_param.h> 61 62#include <i386/bios/apm.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(dev) (minor(dev)&0x0f) 83#define APMDEV_NORMAL 0 84#define APMDEV_CTL 8 85 86#ifdef PC98 87extern int bios32_apm98(struct bios_regs *, u_int, u_short); 88 89/* PC98's SMM definition */ 90#define APM_NECSMM_PORT 0x6b8e 91#define APM_NECSMM_PORTSZ 1 92#define APM_NECSMM_EN 0x10 93static __inline void apm_enable_smm(struct apm_softc *); 94static __inline void apm_disable_smm(struct apm_softc *); 95int apm_necsmm_addr; 96u_int32_t apm_necsmm_mask; 97#endif 98 99static struct apmhook *hook[NAPM_HOOK]; /* XXX */ 100 101#define is_enabled(foo) ((foo) ? "enabled" : "disabled") 102 103/* Map version number to integer (keeps ordering of version numbers) */ 104#define INTVERSION(major, minor) ((major)*100 + (minor)) 105 106static d_open_t apmopen; 107static d_close_t apmclose; 108static d_write_t apmwrite; 109static d_ioctl_t apmioctl; 110static d_poll_t apmpoll; 111 112static struct cdevsw apm_cdevsw = { 113 .d_version = D_VERSION, 114 .d_flags = D_NEEDGIANT, 115 .d_open = apmopen, 116 .d_close = apmclose, 117 .d_write = apmwrite, 118 .d_ioctl = apmioctl, 119 .d_poll = apmpoll, 120 .d_name = "apm", 121}; 122 123static int apm_suspend_delay = 1; 124static int apm_standby_delay = 1; 125static int apm_swab_batt_minutes = 0; 126static int apm_debug = 0; 127 128#define APM_DPRINT(args...) do { \ 129 if (apm_debug) { \ 130 printf(args); \ 131 } \ 132} while (0) 133 134SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, ""); 135SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, ""); 136SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, ""); 137 138TUNABLE_INT("machdep.apm_swab_batt_minutes", &apm_swab_batt_minutes); 139SYSCTL_INT(_machdep, OID_AUTO, apm_swab_batt_minutes, CTLFLAG_RW, 140 &apm_swab_batt_minutes, 0, "Byte swap battery time value."); 141 142#ifdef PC98 143static __inline void 144apm_enable_smm(sc) 145 struct apm_softc *sc; 146{ 147 bus_space_tag_t iot = sc->sc_iot; 148 bus_space_handle_t ioh = sc->sc_ioh; 149 if (apm_necsmm_addr != 0) 150 bus_space_write_1(iot, ioh, 0, 151 (bus_space_read_1(iot, ioh, 0) | ~apm_necsmm_mask)); 152} 153 154static __inline void 155apm_disable_smm(sc) 156 struct apm_softc *sc; 157{ 158 bus_space_tag_t iot = sc->sc_iot; 159 bus_space_handle_t ioh = sc->sc_ioh; 160 if (apm_necsmm_addr != 0) 161 bus_space_write_1(iot, ioh, 0, 162 (bus_space_read_1(iot, ioh, 0) & apm_necsmm_mask)); 163} 164#endif 165 166/* 167 * return 0 if the function successfull, 168 * return 1 if the function unsuccessfull, 169 * return -1 if the function unsupported. 170 */ 171static int 172apm_bioscall(void) 173{ 174 struct apm_softc *sc = &apm_softc; 175 int errno = 0; 176 u_int apm_func = sc->bios.r.eax & 0xff; 177 178 if (!apm_check_function_supported(sc->intversion, apm_func)) { 179 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n", 180 apm_func, sc->majorversion, sc->minorversion); 181 return (-1); 182 } 183 184 sc->bios_busy = 1; 185#ifdef PC98 186 set_bios_selectors(&sc->bios.seg, BIOSCODE_FLAG | BIOSDATA_FLAG); 187 if (bios32_apm98(&sc->bios.r, sc->bios.entry, 188 GSEL(GBIOSCODE32_SEL, SEL_KPL)) != 0) 189 return 1; 190#else 191 if (sc->connectmode == APM_PROT32CONNECT) { 192 set_bios_selectors(&sc->bios.seg, 193 BIOSCODE_FLAG | BIOSDATA_FLAG); 194 errno = bios32(&sc->bios.r, 195 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL)); 196 } else { 197 errno = bios16(&sc->bios, NULL); 198 } 199#endif 200 sc->bios_busy = 0; 201 return (errno); 202} 203 204/* check whether APM function is supported (1) or not (0). */ 205static int 206apm_check_function_supported(u_int version, u_int func) 207{ 208 /* except driver version */ 209 if (func == APM_DRVVERSION) { 210 return (1); 211 } 212#ifdef PC98 213 if (func == APM_GETPWSTATUS) { 214 return (1); 215 } 216#endif 217 218 switch (version) { 219 case INTVERSION(1, 0): 220 if (func > APM_GETPMEVENT) { 221 return (0); /* not supported */ 222 } 223 break; 224 case INTVERSION(1, 1): 225 if (func > APM_ENGAGEDISENGAGEPM && 226 func < APM_OEMFUNC) { 227 return (0); /* not supported */ 228 } 229 break; 230 case INTVERSION(1, 2): 231 break; 232 } 233 234 return (1); /* supported */ 235} 236 237/* enable/disable power management */ 238static int 239apm_enable_disable_pm(int enable) 240{ 241 struct apm_softc *sc = &apm_softc; 242 243 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM; 244 245 if (sc->intversion >= INTVERSION(1, 1)) 246 sc->bios.r.ebx = PMDV_ALLDEV; 247 else 248 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */ 249 sc->bios.r.ecx = enable; 250 sc->bios.r.edx = 0; 251 return (apm_bioscall()); 252} 253 254/* register driver version (APM 1.1 or later) */ 255static int 256apm_driver_version(int version) 257{ 258 struct apm_softc *sc = &apm_softc; 259 260 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION; 261 sc->bios.r.ebx = 0x0; 262 sc->bios.r.ecx = version; 263 sc->bios.r.edx = 0; 264 265 if (apm_bioscall() == 0 && sc->bios.r.eax == version) 266 return (0); 267 268 /* Some old BIOSes don't return the connection version in %ax. */ 269 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION)) 270 return (0); 271 272 return (1); 273} 274 275/* engage/disengage power management (APM 1.1 or later) */ 276static int 277apm_engage_disengage_pm(int engage) 278{ 279 struct apm_softc *sc = &apm_softc; 280 281 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM; 282 sc->bios.r.ebx = PMDV_ALLDEV; 283 sc->bios.r.ecx = engage; 284 sc->bios.r.edx = 0; 285 return (apm_bioscall()); 286} 287 288/* get PM event */ 289static u_int 290apm_getevent(void) 291{ 292 struct apm_softc *sc = &apm_softc; 293 294 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT; 295 296 sc->bios.r.ebx = 0; 297 sc->bios.r.ecx = 0; 298 sc->bios.r.edx = 0; 299 if (apm_bioscall()) 300 return (PMEV_NOEVENT); 301 return (sc->bios.r.ebx & 0xffff); 302} 303 304/* suspend entire system */ 305static int 306apm_suspend_system(int state) 307{ 308 struct apm_softc *sc = &apm_softc; 309 310 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; 311 sc->bios.r.ebx = PMDV_ALLDEV; 312 sc->bios.r.ecx = state; 313 sc->bios.r.edx = 0; 314 315#ifdef PC98 316 apm_disable_smm(sc); 317#endif 318 if (apm_bioscall()) { 319 printf("Entire system suspend failure: errcode = %d\n", 320 0xff & (sc->bios.r.eax >> 8)); 321 return 1; 322 } 323#ifdef PC98 324 apm_enable_smm(sc); 325#endif 326 return 0; 327} 328 329/* Display control */ 330/* 331 * Experimental implementation: My laptop machine can't handle this function 332 * If your laptop can control the display via APM, please inform me. 333 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org> 334 */ 335int 336apm_display(int newstate) 337{ 338 struct apm_softc *sc = &apm_softc; 339 340 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; 341 sc->bios.r.ebx = PMDV_DISP0; 342 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND; 343 sc->bios.r.edx = 0; 344 if (apm_bioscall() == 0) { 345 return 0; 346 } 347 348 /* If failed, then try to blank all display devices instead. */ 349 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; 350 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */ 351 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND; 352 sc->bios.r.edx = 0; 353 if (apm_bioscall() == 0) { 354 return 0; 355 } 356 printf("Display off failure: errcode = %d\n", 357 0xff & (sc->bios.r.eax >> 8)); 358 return 1; 359} 360 361/* 362 * Turn off the entire system. 363 */ 364static void 365apm_power_off(void *junk, int howto) 366{ 367 struct apm_softc *sc = &apm_softc; 368 369 /* Not halting powering off, or not active */ 370 if (!(howto & RB_POWEROFF) || !apm_softc.active) 371 return; 372 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; 373 sc->bios.r.ebx = PMDV_ALLDEV; 374 sc->bios.r.ecx = PMST_OFF; 375 sc->bios.r.edx = 0; 376 (void) apm_bioscall(); 377} 378 379/* APM Battery low handler */ 380static void 381apm_battery_low(void) 382{ 383 printf("\007\007 * * * BATTERY IS LOW * * * \007\007"); 384} 385 386/* APM hook manager */ 387static struct apmhook * 388apm_add_hook(struct apmhook **list, struct apmhook *ah) 389{ 390 int s; 391 struct apmhook *p, *prev; 392 393 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name); 394 395 s = splhigh(); 396 if (ah == NULL) 397 panic("illegal apm_hook!"); 398 prev = NULL; 399 for (p = *list; p != NULL; prev = p, p = p->ah_next) 400 if (p->ah_order > ah->ah_order) 401 break; 402 403 if (prev == NULL) { 404 ah->ah_next = *list; 405 *list = ah; 406 } else { 407 ah->ah_next = prev->ah_next; 408 prev->ah_next = ah; 409 } 410 splx(s); 411 return ah; 412} 413 414static void 415apm_del_hook(struct apmhook **list, struct apmhook *ah) 416{ 417 int s; 418 struct apmhook *p, *prev; 419 420 s = splhigh(); 421 prev = NULL; 422 for (p = *list; p != NULL; prev = p, p = p->ah_next) 423 if (p == ah) 424 goto deleteit; 425 panic("Tried to delete unregistered apm_hook."); 426 goto nosuchnode; 427deleteit: 428 if (prev != NULL) 429 prev->ah_next = p->ah_next; 430 else 431 *list = p->ah_next; 432nosuchnode: 433 splx(s); 434} 435 436 437/* APM driver calls some functions automatically */ 438static void 439apm_execute_hook(struct apmhook *list) 440{ 441 struct apmhook *p; 442 443 for (p = list; p != NULL; p = p->ah_next) { 444 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name); 445 if ((*(p->ah_fun))(p->ah_arg)) 446 printf("Warning: APM hook \"%s\" failed", p->ah_name); 447 } 448} 449 450 451/* establish an apm hook */ 452struct apmhook * 453apm_hook_establish(int apmh, struct apmhook *ah) 454{ 455 if (apmh < 0 || apmh >= NAPM_HOOK) 456 return NULL; 457 458 return apm_add_hook(&hook[apmh], ah); 459} 460 461/* disestablish an apm hook */ 462void 463apm_hook_disestablish(int apmh, struct apmhook *ah) 464{ 465 if (apmh < 0 || apmh >= NAPM_HOOK) 466 return; 467 468 apm_del_hook(&hook[apmh], ah); 469} 470 471static int apm_record_event(struct apm_softc *, u_int); 472static void apm_processevent(void); 473 474static u_int apm_op_inprog = 0; 475 476static void 477apm_do_suspend(void) 478{ 479 struct apm_softc *sc = &apm_softc; 480 int error; 481 482 if (sc == NULL || sc->initialized == 0) 483 return; 484 485 apm_op_inprog = 0; 486 sc->suspends = sc->suspend_countdown = 0; 487 488 /* 489 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since 490 * non-MPSAFE drivers need this. 491 */ 492 mtx_lock(&Giant); 493 error = DEVICE_SUSPEND(root_bus); 494 mtx_unlock(&Giant); 495 if (error) 496 return; 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 mtx_lock(&Giant); 506 DEVICE_RESUME(root_bus); 507 mtx_unlock(&Giant); 508 } 509 return; 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 return; 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 if (device_get_flags(dev) & 0x20) 1159 statclock_disable = 1; 1160 1161 sc->initialized = 0; 1162 1163 /* Must be externally enabled */ 1164 sc->active = 0; 1165 1166 /* Always call HLT in idle loop */ 1167 sc->always_halt_cpu = 1; 1168 1169 getenv_int("debug.apm_debug", &apm_debug); 1170 1171 /* print bootstrap messages */ 1172 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version); 1173 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n", 1174 sc->bios.seg.code16.base, sc->bios.seg.data.base); 1175 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n", 1176 sc->bios.entry, is_enabled(sc->slow_idle_cpu), 1177 is_enabled(!sc->disabled)); 1178 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n", 1179 sc->bios.seg.code16.limit, sc->bios.seg.data.limit); 1180 1181#ifdef PC98 1182 rid = 0; 1183 sc->sc_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 1184 APM_NECSMM_PORT, ~0, APM_NECSMM_PORTSZ, RF_ACTIVE); 1185 if (sc->sc_res == NULL) 1186 panic("%s: counldn't map I/O ports", device_get_name(dev)); 1187 sc->sc_iot = rman_get_bustag(sc->sc_res); 1188 sc->sc_ioh = rman_get_bushandle(sc->sc_res); 1189 1190 if (apm_version==0x112 || apm_version==0x111 || apm_version==0x110) 1191 apm_necsmm_addr = APM_NECSMM_PORT; 1192 else 1193 apm_necsmm_addr = 0; 1194 apm_necsmm_mask = ~APM_NECSMM_EN; 1195#endif /* PC98 */ 1196 1197 /* 1198 * In one test, apm bios version was 1.02; an attempt to register 1199 * a 1.04 driver resulted in a 1.00 connection! Registering a 1200 * 1.02 driver resulted in a 1.02 connection. 1201 */ 1202 drv_version = apm_version > 0x102 ? 0x102 : apm_version; 1203 for (; drv_version > 0x100; drv_version--) 1204 if (apm_driver_version(drv_version) == 0) 1205 break; 1206 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 + 1207 ((drv_version & 0x000f) >> 0); 1208 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 + 1209 ((apm_version & 0x0f00) >> 8); 1210 1211 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion); 1212 1213 if (sc->intversion >= INTVERSION(1, 1)) 1214 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged)); 1215 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n", 1216 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8), 1217 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0), 1218 sc->majorversion, sc->minorversion); 1219 1220 1221 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu)); 1222 /* enable power management */ 1223 if (sc->disabled) { 1224 if (apm_enable_disable_pm(1)) { 1225 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n", 1226 (sc->bios.r.eax >> 8) & 0xff); 1227 } 1228 } 1229 1230 /* engage power managment (APM 1.1 or later) */ 1231 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) { 1232 if (apm_engage_disengage_pm(1)) { 1233 APM_DPRINT("apm: *Warning* engage function failed err=[%x]", 1234 (sc->bios.r.eax >> 8) & 0xff); 1235 APM_DPRINT(" (Docked or using external power?).\n"); 1236 } 1237 } 1238 1239 /* Power the system off using APM */ 1240 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL, 1241 SHUTDOWN_PRI_LAST); 1242 1243 /* Register APM again to pass the correct argument of pm_func. */ 1244 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, sc); 1245 1246 sc->initialized = 1; 1247 sc->suspending = 0; 1248 sc->running = 0; 1249 1250 make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm"); 1251 make_dev(&apm_cdevsw, 8, 0, 5, 0660, "apmctl"); 1252 return 0; 1253} 1254 1255static int 1256apmopen(struct cdev *dev, int flag, int fmt, struct thread *td) 1257{ 1258 struct apm_softc *sc = &apm_softc; 1259 int ctl = APMDEV(dev); 1260 1261 if (sc == NULL || sc->initialized == 0) 1262 return (ENXIO); 1263 1264 switch (ctl) { 1265 case APMDEV_CTL: 1266 if (!(flag & FWRITE)) 1267 return EINVAL; 1268 if (sc->sc_flags & SCFLAG_OCTL) 1269 return EBUSY; 1270 sc->sc_flags |= SCFLAG_OCTL; 1271 bzero(sc->event_filter, sizeof sc->event_filter); 1272 break; 1273 case APMDEV_NORMAL: 1274 sc->sc_flags |= SCFLAG_ONORMAL; 1275 break; 1276 default: 1277 return ENXIO; 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 int ctl = APMDEV(dev); 1288 1289 switch (ctl) { 1290 case APMDEV_CTL: 1291 apm_lastreq_rejected(); 1292 sc->sc_flags &= ~SCFLAG_OCTL; 1293 bzero(sc->event_filter, sizeof sc->event_filter); 1294 break; 1295 case APMDEV_NORMAL: 1296 sc->sc_flags &= ~SCFLAG_ONORMAL; 1297 break; 1298 } 1299 if ((sc->sc_flags & SCFLAG_OPEN) == 0) { 1300 sc->event_count = 0; 1301 sc->event_ptr = 0; 1302 } 1303 return 0; 1304} 1305 1306static int 1307apmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 1308{ 1309 struct apm_softc *sc = &apm_softc; 1310 struct apm_bios_arg *args; 1311 int error = 0; 1312 int ret; 1313 int newstate; 1314 1315 if (sc == NULL || sc->initialized == 0) 1316 return (ENXIO); 1317 1318 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd); 1319 switch (cmd) { 1320 case APMIO_SUSPEND: 1321 if (!(flag & FWRITE)) 1322 return (EPERM); 1323 if (sc->active) 1324 apm_suspend(PMST_SUSPEND); 1325 else 1326 error = EINVAL; 1327 break; 1328 1329 case APMIO_STANDBY: 1330 if (!(flag & FWRITE)) 1331 return (EPERM); 1332 if (sc->active) 1333 apm_suspend(PMST_STANDBY); 1334 else 1335 error = EINVAL; 1336 break; 1337 1338 case APMIO_GETINFO_OLD: 1339 { 1340 struct apm_info info; 1341 apm_info_old_t aiop; 1342 1343 if (apm_get_info(&info)) 1344 error = ENXIO; 1345 aiop = (apm_info_old_t)addr; 1346 aiop->ai_major = info.ai_major; 1347 aiop->ai_minor = info.ai_minor; 1348 aiop->ai_acline = info.ai_acline; 1349 aiop->ai_batt_stat = info.ai_batt_stat; 1350 aiop->ai_batt_life = info.ai_batt_life; 1351 aiop->ai_status = info.ai_status; 1352 } 1353 break; 1354 case APMIO_GETINFO: 1355 if (apm_get_info((apm_info_t)addr)) 1356 error = ENXIO; 1357 break; 1358 case APMIO_GETPWSTATUS: 1359 if (apm_get_pwstatus((apm_pwstatus_t)addr)) 1360 error = ENXIO; 1361 break; 1362 case APMIO_ENABLE: 1363 if (!(flag & FWRITE)) 1364 return (EPERM); 1365 apm_event_enable(); 1366 break; 1367 case APMIO_DISABLE: 1368 if (!(flag & FWRITE)) 1369 return (EPERM); 1370 apm_event_disable(); 1371 break; 1372 case APMIO_HALTCPU: 1373 if (!(flag & FWRITE)) 1374 return (EPERM); 1375 apm_halt_cpu(); 1376 break; 1377 case APMIO_NOTHALTCPU: 1378 if (!(flag & FWRITE)) 1379 return (EPERM); 1380 apm_not_halt_cpu(); 1381 break; 1382 case APMIO_DISPLAY: 1383 if (!(flag & FWRITE)) 1384 return (EPERM); 1385 newstate = *(int *)addr; 1386 if (apm_display(newstate)) 1387 error = ENXIO; 1388 break; 1389 case APMIO_BIOS: 1390 if (!(flag & FWRITE)) 1391 return (EPERM); 1392 /* XXX compatibility with the old interface */ 1393 args = (struct apm_bios_arg *)addr; 1394 sc->bios.r.eax = args->eax; 1395 sc->bios.r.ebx = args->ebx; 1396 sc->bios.r.ecx = args->ecx; 1397 sc->bios.r.edx = args->edx; 1398 sc->bios.r.esi = args->esi; 1399 sc->bios.r.edi = args->edi; 1400 if ((ret = apm_bioscall())) { 1401 /* 1402 * Return code 1 means bios call was unsuccessful. 1403 * Error code is stored in %ah. 1404 * Return code -1 means bios call was unsupported 1405 * in the APM BIOS version. 1406 */ 1407 if (ret == -1) { 1408 error = EINVAL; 1409 } 1410 } else { 1411 /* 1412 * Return code 0 means bios call was successful. 1413 * We need only %al and can discard %ah. 1414 */ 1415 sc->bios.r.eax &= 0xff; 1416 } 1417 args->eax = sc->bios.r.eax; 1418 args->ebx = sc->bios.r.ebx; 1419 args->ecx = sc->bios.r.ecx; 1420 args->edx = sc->bios.r.edx; 1421 args->esi = sc->bios.r.esi; 1422 args->edi = sc->bios.r.edi; 1423 break; 1424 default: 1425 error = EINVAL; 1426 break; 1427 } 1428 1429 /* for /dev/apmctl */ 1430 if (APMDEV(dev) == APMDEV_CTL) { 1431 struct apm_event_info *evp; 1432 int i; 1433 1434 error = 0; 1435 switch (cmd) { 1436 case APMIO_NEXTEVENT: 1437 if (!sc->event_count) { 1438 error = EAGAIN; 1439 } else { 1440 evp = (struct apm_event_info *)addr; 1441 i = sc->event_ptr + APM_NEVENTS - sc->event_count; 1442 i %= APM_NEVENTS; 1443 *evp = sc->event_list[i]; 1444 sc->event_count--; 1445 } 1446 break; 1447 case APMIO_REJECTLASTREQ: 1448 if (apm_lastreq_rejected()) { 1449 error = EINVAL; 1450 } 1451 break; 1452 default: 1453 error = EINVAL; 1454 break; 1455 } 1456 } 1457 1458 return error; 1459} 1460 1461static int 1462apmwrite(struct cdev *dev, struct uio *uio, int ioflag) 1463{ 1464 struct apm_softc *sc = &apm_softc; 1465 u_int event_type; 1466 int error; 1467 u_char enabled; 1468 1469 if (APMDEV(dev) != APMDEV_CTL) 1470 return(ENODEV); 1471 if (uio->uio_resid != sizeof(u_int)) 1472 return(E2BIG); 1473 1474 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio))) 1475 return(error); 1476 1477 if (event_type < 0 || event_type >= APM_NPMEV) 1478 return(EINVAL); 1479 1480 if (sc->event_filter[event_type] == 0) { 1481 enabled = 1; 1482 } else { 1483 enabled = 0; 1484 } 1485 sc->event_filter[event_type] = enabled; 1486 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled)); 1487 1488 return uio->uio_resid; 1489} 1490 1491static int 1492apmpoll(struct cdev *dev, int events, struct thread *td) 1493{ 1494 struct apm_softc *sc = &apm_softc; 1495 int revents = 0; 1496 1497 if (events & (POLLIN | POLLRDNORM)) { 1498 if (sc->event_count) { 1499 revents |= events & (POLLIN | POLLRDNORM); 1500 } else { 1501 selrecord(td, &sc->sc_rsel); 1502 } 1503 } 1504 1505 return (revents); 1506} 1507 1508static device_method_t apm_methods[] = { 1509 /* Device interface */ 1510 DEVMETHOD(device_identify, apm_identify), 1511 DEVMETHOD(device_probe, apm_probe), 1512 DEVMETHOD(device_attach, apm_attach), 1513 1514 { 0, 0 } 1515}; 1516 1517static driver_t apm_driver = { 1518 "apm", 1519 apm_methods, 1520 1, /* no softc (XXX) */ 1521}; 1522 1523static devclass_t apm_devclass; 1524 1525DRIVER_MODULE(apm, legacy, apm_driver, apm_devclass, apm_modevent, 0); 1526MODULE_VERSION(apm, 1); 1527 1528static int 1529apm_pm_func(u_long cmd, void *arg, ...) 1530{ 1531 int state, apm_state; 1532 int error; 1533 va_list ap; 1534 1535 error = 0; 1536 switch (cmd) { 1537 case POWER_CMD_SUSPEND: 1538 va_start(ap, arg); 1539 state = va_arg(ap, int); 1540 va_end(ap); 1541 1542 switch (state) { 1543 case POWER_SLEEP_STATE_STANDBY: 1544 apm_state = PMST_STANDBY; 1545 break; 1546 case POWER_SLEEP_STATE_SUSPEND: 1547 case POWER_SLEEP_STATE_HIBERNATE: 1548 apm_state = PMST_SUSPEND; 1549 break; 1550 default: 1551 error = EINVAL; 1552 goto out; 1553 } 1554 1555 apm_suspend(apm_state); 1556 break; 1557 1558 default: 1559 error = EINVAL; 1560 goto out; 1561 } 1562 1563out: 1564 return (error); 1565} 1566 1567static void 1568apm_pm_register(void *arg) 1569{ 1570 1571 if (!resource_disabled("apm", 0)) 1572 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, NULL); 1573} 1574 1575SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, apm_pm_register, 0); 1576