| 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 | * $FreeBSD: src/sys/i386/apm/apm.c,v 1.114.2.5 2002/11/02 04:41:50 iwasaki Exp $ |
| 19 | * $DragonFly: src/sys/platform/pc32/apm/apm.c,v 1.17 2006/09/10 01:26:38 dillon Exp $ |
| 20 | */ |
| 21 | |
| 22 | #include <sys/param.h> |
| 23 | #include <sys/systm.h> |
| 24 | #include <sys/eventhandler.h> |
| 25 | #include <sys/conf.h> |
| 26 | #include <sys/device.h> |
| 27 | #include <sys/kernel.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/reboot.h> |
| 30 | #include <sys/bus.h> |
| 31 | #include <sys/selinfo.h> |
| 32 | #include <sys/poll.h> |
| 33 | #include <sys/fcntl.h> |
| 34 | #include <sys/uio.h> |
| 35 | #include <sys/signalvar.h> |
| 36 | #include <sys/sysctl.h> |
| 37 | #include <machine/apm_bios.h> |
| 38 | #include <machine/segments.h> |
| 39 | #include <machine/clock.h> |
| 40 | #include <vm/vm.h> |
| 41 | #include <vm/vm_param.h> |
| 42 | #include <vm/pmap.h> |
| 43 | #include <sys/syslog.h> |
| 44 | #include <sys/thread2.h> |
| 45 | |
| 46 | #include <machine/pc/bios.h> |
| 47 | #include <machine/vm86.h> |
| 48 | |
| 49 | #include <i386/apm/apm.h> |
| 50 | |
| 51 | /* Used by the apm_saver screen saver module */ |
| 52 | int apm_display (int newstate); |
| 53 | struct apm_softc apm_softc; |
| 54 | |
| 55 | static void apm_resume (void); |
| 56 | static int apm_bioscall(void); |
| 57 | static int apm_check_function_supported (u_int version, u_int func); |
| 58 | |
| 59 | static u_long apm_version; |
| 60 | |
| 61 | int apm_evindex; |
| 62 | |
| 63 | #define SCFLAG_ONORMAL 0x0000001 |
| 64 | #define SCFLAG_OCTL 0x0000002 |
| 65 | #define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL) |
| 66 | |
| 67 | #define APMDEV(dev) (minor(dev)&0x0f) |
| 68 | #define APMDEV_NORMAL 0 |
| 69 | #define APMDEV_CTL 8 |
| 70 | |
| 71 | static struct apmhook *hook[NAPM_HOOK]; /* XXX */ |
| 72 | |
| 73 | #define is_enabled(foo) ((foo) ? "enabled" : "disabled") |
| 74 | |
| 75 | /* Map version number to integer (keeps ordering of version numbers) */ |
| 76 | #define INTVERSION(major, minor) ((major)*100 + (minor)) |
| 77 | |
| 78 | static struct callout apm_timeout_ch; |
| 79 | |
| 80 | static timeout_t apm_timeout; |
| 81 | static d_open_t apmopen; |
| 82 | static d_close_t apmclose; |
| 83 | static d_write_t apmwrite; |
| 84 | static d_ioctl_t apmioctl; |
| 85 | static d_poll_t apmpoll; |
| 86 | |
| 87 | #define CDEV_MAJOR 39 |
| 88 | static struct dev_ops apm_ops = { |
| 89 | { "apm", CDEV_MAJOR, 0 }, |
| 90 | .d_open = apmopen, |
| 91 | .d_close = apmclose, |
| 92 | .d_write = apmwrite, |
| 93 | .d_ioctl = apmioctl, |
| 94 | .d_poll = apmpoll, |
| 95 | }; |
| 96 | |
| 97 | static int apm_suspend_delay = 1; |
| 98 | static int apm_standby_delay = 1; |
| 99 | static int apm_debug = 0; |
| 100 | |
| 101 | #define APM_DPRINT(args...) do { \ |
| 102 | if (apm_debug) { \ |
| 103 | printf(args); \ |
| 104 | } \ |
| 105 | } while (0) |
| 106 | |
| 107 | SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, ""); |
| 108 | SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, ""); |
| 109 | SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, ""); |
| 110 | |
| 111 | /* |
| 112 | * return 0 if the function successfull, |
| 113 | * return 1 if the function unsuccessfull, |
| 114 | * return -1 if the function unsupported. |
| 115 | */ |
| 116 | static int |
| 117 | apm_bioscall(void) |
| 118 | { |
| 119 | struct apm_softc *sc = &apm_softc; |
| 120 | int errno = 0; |
| 121 | u_int apm_func = sc->bios.r.eax & 0xff; |
| 122 | |
| 123 | if (!apm_check_function_supported(sc->intversion, apm_func)) { |
| 124 | APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n", |
| 125 | apm_func, sc->majorversion, sc->minorversion); |
| 126 | return (-1); |
| 127 | } |
| 128 | |
| 129 | sc->bios_busy = 1; |
| 130 | if (sc->connectmode == APM_PROT32CONNECT) { |
| 131 | set_bios_selectors(&sc->bios.seg, |
| 132 | BIOSCODE_FLAG | BIOSDATA_FLAG); |
| 133 | errno = bios32(&sc->bios.r, |
| 134 | sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL)); |
| 135 | } else { |
| 136 | errno = bios16(&sc->bios, NULL); |
| 137 | } |
| 138 | sc->bios_busy = 0; |
| 139 | return (errno); |
| 140 | } |
| 141 | |
| 142 | /* check whether APM function is supported (1) or not (0). */ |
| 143 | static int |
| 144 | apm_check_function_supported(u_int version, u_int func) |
| 145 | { |
| 146 | /* except driver version */ |
| 147 | if (func == APM_DRVVERSION) { |
| 148 | return (1); |
| 149 | } |
| 150 | |
| 151 | switch (version) { |
| 152 | case INTVERSION(1, 0): |
| 153 | if (func > APM_GETPMEVENT) { |
| 154 | return (0); /* not supported */ |
| 155 | } |
| 156 | break; |
| 157 | case INTVERSION(1, 1): |
| 158 | if (func > APM_ENGAGEDISENGAGEPM && |
| 159 | func < APM_OEMFUNC) { |
| 160 | return (0); /* not supported */ |
| 161 | } |
| 162 | break; |
| 163 | case INTVERSION(1, 2): |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | return (1); /* supported */ |
| 168 | } |
| 169 | |
| 170 | /* enable/disable power management */ |
| 171 | static int |
| 172 | apm_enable_disable_pm(int enable) |
| 173 | { |
| 174 | struct apm_softc *sc = &apm_softc; |
| 175 | |
| 176 | sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM; |
| 177 | |
| 178 | if (sc->intversion >= INTVERSION(1, 1)) |
| 179 | sc->bios.r.ebx = PMDV_ALLDEV; |
| 180 | else |
| 181 | sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */ |
| 182 | sc->bios.r.ecx = enable; |
| 183 | sc->bios.r.edx = 0; |
| 184 | return (apm_bioscall()); |
| 185 | } |
| 186 | |
| 187 | /* register driver version (APM 1.1 or later) */ |
| 188 | static int |
| 189 | apm_driver_version(int version) |
| 190 | { |
| 191 | struct apm_softc *sc = &apm_softc; |
| 192 | |
| 193 | sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION; |
| 194 | sc->bios.r.ebx = 0x0; |
| 195 | sc->bios.r.ecx = version; |
| 196 | sc->bios.r.edx = 0; |
| 197 | |
| 198 | if (apm_bioscall() == 0 && sc->bios.r.eax == version) |
| 199 | return (0); |
| 200 | |
| 201 | /* Some old BIOSes don't return the connection version in %ax. */ |
| 202 | if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION)) |
| 203 | return (0); |
| 204 | |
| 205 | return (1); |
| 206 | } |
| 207 | |
| 208 | /* engage/disengage power management (APM 1.1 or later) */ |
| 209 | static int |
| 210 | apm_engage_disengage_pm(int engage) |
| 211 | { |
| 212 | struct apm_softc *sc = &apm_softc; |
| 213 | |
| 214 | sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM; |
| 215 | sc->bios.r.ebx = PMDV_ALLDEV; |
| 216 | sc->bios.r.ecx = engage; |
| 217 | sc->bios.r.edx = 0; |
| 218 | return (apm_bioscall()); |
| 219 | } |
| 220 | |
| 221 | /* get PM event */ |
| 222 | static u_int |
| 223 | apm_getevent(void) |
| 224 | { |
| 225 | struct apm_softc *sc = &apm_softc; |
| 226 | |
| 227 | sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT; |
| 228 | |
| 229 | sc->bios.r.ebx = 0; |
| 230 | sc->bios.r.ecx = 0; |
| 231 | sc->bios.r.edx = 0; |
| 232 | if (apm_bioscall()) |
| 233 | return (PMEV_NOEVENT); |
| 234 | return (sc->bios.r.ebx & 0xffff); |
| 235 | } |
| 236 | |
| 237 | /* suspend entire system */ |
| 238 | static int |
| 239 | apm_suspend_system(int state) |
| 240 | { |
| 241 | struct apm_softc *sc = &apm_softc; |
| 242 | |
| 243 | sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; |
| 244 | sc->bios.r.ebx = PMDV_ALLDEV; |
| 245 | sc->bios.r.ecx = state; |
| 246 | sc->bios.r.edx = 0; |
| 247 | |
| 248 | if (apm_bioscall()) { |
| 249 | printf("Entire system suspend failure: errcode = %d\n", |
| 250 | 0xff & (sc->bios.r.eax >> 8)); |
| 251 | return 1; |
| 252 | } |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /* Display control */ |
| 257 | /* |
| 258 | * Experimental implementation: My laptop machine can't handle this function |
| 259 | * If your laptop can control the display via APM, please inform me. |
| 260 | * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org> |
| 261 | */ |
| 262 | int |
| 263 | apm_display(int newstate) |
| 264 | { |
| 265 | struct apm_softc *sc = &apm_softc; |
| 266 | |
| 267 | sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; |
| 268 | sc->bios.r.ebx = PMDV_DISP0; |
| 269 | sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND; |
| 270 | sc->bios.r.edx = 0; |
| 271 | if (apm_bioscall() == 0) { |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | /* If failed, then try to blank all display devices instead. */ |
| 276 | sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; |
| 277 | sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */ |
| 278 | sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND; |
| 279 | sc->bios.r.edx = 0; |
| 280 | if (apm_bioscall() == 0) { |
| 281 | return 0; |
| 282 | } |
| 283 | printf("Display off failure: errcode = %d\n", |
| 284 | 0xff & (sc->bios.r.eax >> 8)); |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Turn off the entire system. |
| 290 | */ |
| 291 | static void |
| 292 | apm_power_off(void *junk, int howto) |
| 293 | { |
| 294 | struct apm_softc *sc = &apm_softc; |
| 295 | |
| 296 | /* Not halting powering off, or not active */ |
| 297 | if (!(howto & RB_POWEROFF) || !apm_softc.active) |
| 298 | return; |
| 299 | sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; |
| 300 | sc->bios.r.ebx = PMDV_ALLDEV; |
| 301 | sc->bios.r.ecx = PMST_OFF; |
| 302 | sc->bios.r.edx = 0; |
| 303 | apm_bioscall(); |
| 304 | } |
| 305 | |
| 306 | /* APM Battery low handler */ |
| 307 | static void |
| 308 | apm_battery_low(void) |
| 309 | { |
| 310 | printf("\007\007 * * * BATTERY IS LOW * * * \007\007"); |
| 311 | } |
| 312 | |
| 313 | /* APM hook manager */ |
| 314 | static struct apmhook * |
| 315 | apm_add_hook(struct apmhook **list, struct apmhook *ah) |
| 316 | { |
| 317 | struct apmhook *p, *prev; |
| 318 | |
| 319 | APM_DPRINT("Add hook \"%s\"\n", ah->ah_name); |
| 320 | |
| 321 | crit_enter(); |
| 322 | if (ah == NULL) |
| 323 | panic("illegal apm_hook!"); |
| 324 | prev = NULL; |
| 325 | for (p = *list; p != NULL; prev = p, p = p->ah_next) |
| 326 | if (p->ah_order > ah->ah_order) |
| 327 | break; |
| 328 | |
| 329 | if (prev == NULL) { |
| 330 | ah->ah_next = *list; |
| 331 | *list = ah; |
| 332 | } else { |
| 333 | ah->ah_next = prev->ah_next; |
| 334 | prev->ah_next = ah; |
| 335 | } |
| 336 | crit_exit(); |
| 337 | return ah; |
| 338 | } |
| 339 | |
| 340 | static void |
| 341 | apm_del_hook(struct apmhook **list, struct apmhook *ah) |
| 342 | { |
| 343 | struct apmhook *p, *prev; |
| 344 | |
| 345 | crit_enter(); |
| 346 | prev = NULL; |
| 347 | for (p = *list; p != NULL; prev = p, p = p->ah_next) |
| 348 | if (p == ah) |
| 349 | goto deleteit; |
| 350 | panic("Tried to delete unregistered apm_hook."); |
| 351 | goto nosuchnode; |
| 352 | deleteit: |
| 353 | if (prev != NULL) |
| 354 | prev->ah_next = p->ah_next; |
| 355 | else |
| 356 | *list = p->ah_next; |
| 357 | nosuchnode: |
| 358 | crit_exit(); |
| 359 | } |
| 360 | |
| 361 | |
| 362 | /* APM driver calls some functions automatically */ |
| 363 | static void |
| 364 | apm_execute_hook(struct apmhook *list) |
| 365 | { |
| 366 | struct apmhook *p; |
| 367 | |
| 368 | for (p = list; p != NULL; p = p->ah_next) { |
| 369 | APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name); |
| 370 | if ((*(p->ah_fun))(p->ah_arg)) |
| 371 | printf("Warning: APM hook \"%s\" failed", p->ah_name); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /* establish an apm hook */ |
| 377 | struct apmhook * |
| 378 | apm_hook_establish(int apmh, struct apmhook *ah) |
| 379 | { |
| 380 | if (apmh < 0 || apmh >= NAPM_HOOK) |
| 381 | return NULL; |
| 382 | |
| 383 | return apm_add_hook(&hook[apmh], ah); |
| 384 | } |
| 385 | |
| 386 | /* disestablish an apm hook */ |
| 387 | void |
| 388 | apm_hook_disestablish(int apmh, struct apmhook *ah) |
| 389 | { |
| 390 | if (apmh < 0 || apmh >= NAPM_HOOK) |
| 391 | return; |
| 392 | |
| 393 | apm_del_hook(&hook[apmh], ah); |
| 394 | } |
| 395 | |
| 396 | |
| 397 | static struct timeval suspend_time; |
| 398 | static struct timeval diff_time; |
| 399 | |
| 400 | static int |
| 401 | apm_default_resume(void *arg) |
| 402 | { |
| 403 | u_int second, minute, hour; |
| 404 | struct timeval resume_time, tmp_time; |
| 405 | |
| 406 | /* modified for adjkerntz */ |
| 407 | crit_enter(); |
| 408 | timer_restore(); /* restore the all timers */ |
| 409 | inittodr(0); /* adjust time to RTC */ |
| 410 | microtime(&resume_time); |
| 411 | getmicrotime(&tmp_time); |
| 412 | timevaladd(&tmp_time, &diff_time); |
| 413 | |
| 414 | #ifdef FIXME |
| 415 | /* XXX THIS DOESN'T WORK!!! */ |
| 416 | time = tmp_time; |
| 417 | #endif |
| 418 | |
| 419 | #ifdef APM_FIXUP_CALLTODO |
| 420 | /* Calculate the delta time suspended */ |
| 421 | timevalsub(&resume_time, &suspend_time); |
| 422 | /* Fixup the calltodo list with the delta time. */ |
| 423 | adjust_timeout_calltodo(&resume_time); |
| 424 | #endif /* APM_FIXUP_CALLTODOK */ |
| 425 | crit_exit(); |
| 426 | #ifndef APM_FIXUP_CALLTODO |
| 427 | second = resume_time.tv_sec - suspend_time.tv_sec; |
| 428 | #else /* APM_FIXUP_CALLTODO */ |
| 429 | /* |
| 430 | * We've already calculated resume_time to be the delta between |
| 431 | * the suspend and the resume. |
| 432 | */ |
| 433 | second = resume_time.tv_sec; |
| 434 | #endif /* APM_FIXUP_CALLTODO */ |
| 435 | hour = second / 3600; |
| 436 | second %= 3600; |
| 437 | minute = second / 60; |
| 438 | second %= 60; |
| 439 | log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n", |
| 440 | hour, minute, second); |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static int |
| 445 | apm_default_suspend(void *arg) |
| 446 | { |
| 447 | crit_enter(); |
| 448 | microtime(&diff_time); |
| 449 | inittodr(0); |
| 450 | microtime(&suspend_time); |
| 451 | timevalsub(&diff_time, &suspend_time); |
| 452 | crit_exit(); |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static int apm_record_event (struct apm_softc *, u_int); |
| 457 | static void apm_processevent(void); |
| 458 | |
| 459 | static u_int apm_op_inprog = 0; |
| 460 | |
| 461 | static void |
| 462 | apm_do_suspend(void) |
| 463 | { |
| 464 | struct apm_softc *sc = &apm_softc; |
| 465 | int error; |
| 466 | |
| 467 | if (!sc) |
| 468 | return; |
| 469 | |
| 470 | apm_op_inprog = 0; |
| 471 | sc->suspends = sc->suspend_countdown = 0; |
| 472 | |
| 473 | if (sc->initialized) { |
| 474 | error = DEVICE_SUSPEND(root_bus); |
| 475 | if (error) { |
| 476 | DEVICE_RESUME(root_bus); |
| 477 | } else { |
| 478 | apm_execute_hook(hook[APM_HOOK_SUSPEND]); |
| 479 | if (apm_suspend_system(PMST_SUSPEND) == 0) { |
| 480 | apm_processevent(); |
| 481 | } else { |
| 482 | /* Failure, 'resume' the system again */ |
| 483 | apm_execute_hook(hook[APM_HOOK_RESUME]); |
| 484 | DEVICE_RESUME(root_bus); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | static void |
| 491 | apm_do_standby(void) |
| 492 | { |
| 493 | struct apm_softc *sc = &apm_softc; |
| 494 | |
| 495 | if (!sc) |
| 496 | return; |
| 497 | |
| 498 | apm_op_inprog = 0; |
| 499 | sc->standbys = sc->standby_countdown = 0; |
| 500 | |
| 501 | if (sc->initialized) { |
| 502 | /* |
| 503 | * As far as standby, we don't need to execute |
| 504 | * all of suspend hooks. |
| 505 | */ |
| 506 | apm_default_suspend(&apm_softc); |
| 507 | if (apm_suspend_system(PMST_STANDBY) == 0) |
| 508 | apm_processevent(); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | static void |
| 513 | apm_lastreq_notify(void) |
| 514 | { |
| 515 | struct apm_softc *sc = &apm_softc; |
| 516 | |
| 517 | sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; |
| 518 | sc->bios.r.ebx = PMDV_ALLDEV; |
| 519 | sc->bios.r.ecx = PMST_LASTREQNOTIFY; |
| 520 | sc->bios.r.edx = 0; |
| 521 | apm_bioscall(); |
| 522 | } |
| 523 | |
| 524 | static int |
| 525 | apm_lastreq_rejected(void) |
| 526 | { |
| 527 | struct apm_softc *sc = &apm_softc; |
| 528 | |
| 529 | if (apm_op_inprog == 0) { |
| 530 | return 1; /* no operation in progress */ |
| 531 | } |
| 532 | |
| 533 | sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE; |
| 534 | sc->bios.r.ebx = PMDV_ALLDEV; |
| 535 | sc->bios.r.ecx = PMST_LASTREQREJECT; |
| 536 | sc->bios.r.edx = 0; |
| 537 | |
| 538 | if (apm_bioscall()) { |
| 539 | APM_DPRINT("apm_lastreq_rejected: failed\n"); |
| 540 | return 1; |
| 541 | } |
| 542 | apm_op_inprog = 0; |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | * Public interface to the suspend/resume: |
| 548 | * |
| 549 | * Execute suspend and resume hook before and after sleep, respectively. |
| 550 | * |
| 551 | */ |
| 552 | |
| 553 | void |
| 554 | apm_suspend(int state) |
| 555 | { |
| 556 | struct apm_softc *sc = &apm_softc; |
| 557 | |
| 558 | if (!sc->initialized) |
| 559 | return; |
| 560 | |
| 561 | switch (state) { |
| 562 | case PMST_SUSPEND: |
| 563 | if (sc->suspends) |
| 564 | return; |
| 565 | sc->suspends++; |
| 566 | sc->suspend_countdown = apm_suspend_delay; |
| 567 | break; |
| 568 | case PMST_STANDBY: |
| 569 | if (sc->standbys) |
| 570 | return; |
| 571 | sc->standbys++; |
| 572 | sc->standby_countdown = apm_standby_delay; |
| 573 | break; |
| 574 | default: |
| 575 | printf("apm_suspend: Unknown Suspend state 0x%x\n", state); |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | apm_op_inprog++; |
| 580 | apm_lastreq_notify(); |
| 581 | } |
| 582 | |
| 583 | void |
| 584 | apm_resume(void) |
| 585 | { |
| 586 | struct apm_softc *sc = &apm_softc; |
| 587 | |
| 588 | if (!sc) |
| 589 | return; |
| 590 | |
| 591 | if (sc->initialized) { |
| 592 | apm_execute_hook(hook[APM_HOOK_RESUME]); |
| 593 | DEVICE_RESUME(root_bus); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | |
| 598 | /* get power status per battery */ |
| 599 | static int |
| 600 | apm_get_pwstatus(apm_pwstatus_t app) |
| 601 | { |
| 602 | struct apm_softc *sc = &apm_softc; |
| 603 | |
| 604 | if (app->ap_device != PMDV_ALLDEV && |
| 605 | (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL)) |
| 606 | return 1; |
| 607 | |
| 608 | sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS; |
| 609 | sc->bios.r.ebx = app->ap_device; |
| 610 | sc->bios.r.ecx = 0; |
| 611 | sc->bios.r.edx = 0xffff; /* default to unknown battery time */ |
| 612 | |
| 613 | if (apm_bioscall()) |
| 614 | return 1; |
| 615 | |
| 616 | app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff; |
| 617 | app->ap_batt_stat = sc->bios.r.ebx & 0xff; |
| 618 | app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff; |
| 619 | app->ap_batt_life = sc->bios.r.ecx & 0xff; |
| 620 | sc->bios.r.edx &= 0xffff; |
| 621 | if (sc->bios.r.edx == 0xffff) /* Time is unknown */ |
| 622 | app->ap_batt_time = -1; |
| 623 | else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */ |
| 624 | app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60; |
| 625 | else /* Time is in seconds */ |
| 626 | app->ap_batt_time = sc->bios.r.edx; |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | |
| 632 | /* get APM information */ |
| 633 | static int |
| 634 | apm_get_info(apm_info_t aip) |
| 635 | { |
| 636 | struct apm_softc *sc = &apm_softc; |
| 637 | struct apm_pwstatus aps; |
| 638 | |
| 639 | bzero(&aps, sizeof(aps)); |
| 640 | aps.ap_device = PMDV_ALLDEV; |
| 641 | if (apm_get_pwstatus(&aps)) |
| 642 | return 1; |
| 643 | |
| 644 | aip->ai_infoversion = 1; |
| 645 | aip->ai_acline = aps.ap_acline; |
| 646 | aip->ai_batt_stat = aps.ap_batt_stat; |
| 647 | aip->ai_batt_life = aps.ap_batt_life; |
| 648 | aip->ai_batt_time = aps.ap_batt_time; |
| 649 | aip->ai_major = (u_int)sc->majorversion; |
| 650 | aip->ai_minor = (u_int)sc->minorversion; |
| 651 | aip->ai_status = (u_int)sc->active; |
| 652 | |
| 653 | sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES; |
| 654 | sc->bios.r.ebx = 0; |
| 655 | sc->bios.r.ecx = 0; |
| 656 | sc->bios.r.edx = 0; |
| 657 | if (apm_bioscall()) { |
| 658 | aip->ai_batteries = -1; /* Unknown */ |
| 659 | aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */ |
| 660 | } else { |
| 661 | aip->ai_batteries = sc->bios.r.ebx & 0xff; |
| 662 | aip->ai_capabilities = sc->bios.r.ecx & 0xf; |
| 663 | } |
| 664 | |
| 665 | bzero(aip->ai_spare, sizeof aip->ai_spare); |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | |
| 671 | /* inform APM BIOS that CPU is idle */ |
| 672 | void |
| 673 | apm_cpu_idle(void) |
| 674 | { |
| 675 | struct apm_softc *sc = &apm_softc; |
| 676 | |
| 677 | if (sc->active) { |
| 678 | |
| 679 | sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE; |
| 680 | sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0; |
| 681 | apm_bioscall(); |
| 682 | } |
| 683 | /* |
| 684 | * Some APM implementation halts CPU in BIOS, whenever |
| 685 | * "CPU-idle" function are invoked, but swtch() of |
| 686 | * FreeBSD halts CPU, therefore, CPU is halted twice |
| 687 | * in the sched loop. It makes the interrupt latency |
| 688 | * terribly long and be able to cause a serious problem |
| 689 | * in interrupt processing. We prevent it by removing |
| 690 | * "hlt" operation from swtch() and managed it under |
| 691 | * APM driver. |
| 692 | */ |
| 693 | if (!sc->active || sc->always_halt_cpu) |
| 694 | __asm("hlt"); /* wait for interrupt */ |
| 695 | } |
| 696 | |
| 697 | /* inform APM BIOS that CPU is busy */ |
| 698 | void |
| 699 | apm_cpu_busy(void) |
| 700 | { |
| 701 | struct apm_softc *sc = &apm_softc; |
| 702 | |
| 703 | /* |
| 704 | * The APM specification says this is only necessary if your BIOS |
| 705 | * slows down the processor in the idle task, otherwise it's not |
| 706 | * necessary. |
| 707 | */ |
| 708 | if (sc->slow_idle_cpu && sc->active) { |
| 709 | |
| 710 | sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY; |
| 711 | sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0; |
| 712 | apm_bioscall(); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | |
| 717 | /* |
| 718 | * APM timeout routine: |
| 719 | * |
| 720 | * This routine is automatically called by timer once per second. |
| 721 | */ |
| 722 | |
| 723 | static void |
| 724 | apm_timeout(void *dummy) |
| 725 | { |
| 726 | struct apm_softc *sc = &apm_softc; |
| 727 | |
| 728 | if (apm_op_inprog) |
| 729 | apm_lastreq_notify(); |
| 730 | |
| 731 | if (sc->standbys && sc->standby_countdown-- <= 0) |
| 732 | apm_do_standby(); |
| 733 | |
| 734 | if (sc->suspends && sc->suspend_countdown-- <= 0) |
| 735 | apm_do_suspend(); |
| 736 | |
| 737 | if (!sc->bios_busy) |
| 738 | apm_processevent(); |
| 739 | |
| 740 | if (sc->active == 1) { |
| 741 | /* Run slightly more oftan than 1 Hz */ |
| 742 | callout_reset(&apm_timeout_ch, hz - 1, apm_timeout, NULL); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | /* enable APM BIOS */ |
| 747 | static void |
| 748 | apm_event_enable(void) |
| 749 | { |
| 750 | struct apm_softc *sc = &apm_softc; |
| 751 | |
| 752 | APM_DPRINT("called apm_event_enable()\n"); |
| 753 | if (sc->initialized) { |
| 754 | sc->active = 1; |
| 755 | callout_init(&apm_timeout_ch); |
| 756 | apm_timeout(sc); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /* disable APM BIOS */ |
| 761 | static void |
| 762 | apm_event_disable(void) |
| 763 | { |
| 764 | struct apm_softc *sc = &apm_softc; |
| 765 | |
| 766 | APM_DPRINT("called apm_event_disable()\n"); |
| 767 | if (sc->initialized) { |
| 768 | callout_stop(&apm_timeout_ch); |
| 769 | sc->active = 0; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | /* halt CPU in scheduling loop */ |
| 774 | static void |
| 775 | apm_halt_cpu(void) |
| 776 | { |
| 777 | struct apm_softc *sc = &apm_softc; |
| 778 | |
| 779 | if (sc->initialized) |
| 780 | sc->always_halt_cpu = 1; |
| 781 | } |
| 782 | |
| 783 | /* don't halt CPU in scheduling loop */ |
| 784 | static void |
| 785 | apm_not_halt_cpu(void) |
| 786 | { |
| 787 | struct apm_softc *sc = &apm_softc; |
| 788 | |
| 789 | if (sc->initialized) |
| 790 | sc->always_halt_cpu = 0; |
| 791 | } |
| 792 | |
| 793 | /* device driver definitions */ |
| 794 | |
| 795 | /* |
| 796 | * probe for APM BIOS |
| 797 | */ |
| 798 | static int |
| 799 | apm_probe(device_t dev) |
| 800 | { |
| 801 | #define APM_KERNBASE KERNBASE |
| 802 | struct vm86frame vmf; |
| 803 | struct apm_softc *sc = &apm_softc; |
| 804 | int disabled, flags; |
| 805 | |
| 806 | if (resource_int_value("apm", 0, "disabled", &disabled) == 0 |
| 807 | && disabled != 0) |
| 808 | return ENXIO; |
| 809 | |
| 810 | device_set_desc(dev, "APM BIOS"); |
| 811 | |
| 812 | if ( device_get_unit(dev) > 0 ) { |
| 813 | printf("apm: Only one APM driver supported.\n"); |
| 814 | return ENXIO; |
| 815 | } |
| 816 | |
| 817 | if (resource_int_value("apm", 0, "flags", &flags) != 0) |
| 818 | flags = 0; |
| 819 | |
| 820 | bzero(&vmf, sizeof(struct vm86frame)); /* safety */ |
| 821 | bzero(&apm_softc, sizeof(apm_softc)); |
| 822 | vmf.vmf_ah = APM_BIOS; |
| 823 | vmf.vmf_al = APM_INSTCHECK; |
| 824 | vmf.vmf_bx = 0; |
| 825 | if (vm86_intcall(APM_INT, &vmf)) |
| 826 | return ENXIO; /* APM not found */ |
| 827 | if (vmf.vmf_bx != 0x504d) { |
| 828 | printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx); |
| 829 | return ENXIO; |
| 830 | } |
| 831 | if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) { |
| 832 | printf("apm: protected mode connections are not supported\n"); |
| 833 | return ENXIO; |
| 834 | } |
| 835 | |
| 836 | apm_version = vmf.vmf_ax; |
| 837 | sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0); |
| 838 | sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0); |
| 839 | sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0); |
| 840 | |
| 841 | vmf.vmf_ah = APM_BIOS; |
| 842 | vmf.vmf_al = APM_DISCONNECT; |
| 843 | vmf.vmf_bx = 0; |
| 844 | vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */ |
| 845 | |
| 846 | if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) { |
| 847 | vmf.vmf_ah = APM_BIOS; |
| 848 | vmf.vmf_al = APM_PROT32CONNECT; |
| 849 | vmf.vmf_bx = 0; |
| 850 | if (vm86_intcall(APM_INT, &vmf)) { |
| 851 | printf("apm: 32-bit connection error.\n"); |
| 852 | return (ENXIO); |
| 853 | } |
| 854 | sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE; |
| 855 | sc->bios.seg.code32.limit = 0xffff; |
| 856 | sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE; |
| 857 | sc->bios.seg.code16.limit = 0xffff; |
| 858 | sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE; |
| 859 | sc->bios.seg.data.limit = 0xffff; |
| 860 | sc->bios.entry = vmf.vmf_ebx; |
| 861 | sc->connectmode = APM_PROT32CONNECT; |
| 862 | } else { |
| 863 | /* use 16-bit connection */ |
| 864 | vmf.vmf_ah = APM_BIOS; |
| 865 | vmf.vmf_al = APM_PROT16CONNECT; |
| 866 | vmf.vmf_bx = 0; |
| 867 | if (vm86_intcall(APM_INT, &vmf)) { |
| 868 | printf("apm: 16-bit connection error.\n"); |
| 869 | return (ENXIO); |
| 870 | } |
| 871 | sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE; |
| 872 | sc->bios.seg.code16.limit = 0xffff; |
| 873 | sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE; |
| 874 | sc->bios.seg.data.limit = 0xffff; |
| 875 | sc->bios.entry = vmf.vmf_bx; |
| 876 | sc->connectmode = APM_PROT16CONNECT; |
| 877 | } |
| 878 | return(0); |
| 879 | } |
| 880 | |
| 881 | |
| 882 | /* |
| 883 | * return 0 if the user will notice and handle the event, |
| 884 | * return 1 if the kernel driver should do so. |
| 885 | */ |
| 886 | static int |
| 887 | apm_record_event(struct apm_softc *sc, u_int event_type) |
| 888 | { |
| 889 | struct apm_event_info *evp; |
| 890 | |
| 891 | if ((sc->sc_flags & SCFLAG_OPEN) == 0) |
| 892 | return 1; /* no user waiting */ |
| 893 | if (sc->event_count == APM_NEVENTS) |
| 894 | return 1; /* overflow */ |
| 895 | if (sc->event_filter[event_type] == 0) |
| 896 | return 1; /* not registered */ |
| 897 | evp = &sc->event_list[sc->event_ptr]; |
| 898 | sc->event_count++; |
| 899 | sc->event_ptr++; |
| 900 | sc->event_ptr %= APM_NEVENTS; |
| 901 | evp->type = event_type; |
| 902 | evp->index = ++apm_evindex; |
| 903 | selwakeup(&sc->sc_rsel); |
| 904 | return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */ |
| 905 | } |
| 906 | |
| 907 | /* Process APM event */ |
| 908 | static void |
| 909 | apm_processevent(void) |
| 910 | { |
| 911 | int apm_event; |
| 912 | struct apm_softc *sc = &apm_softc; |
| 913 | |
| 914 | #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \ |
| 915 | APM_DPRINT("Received APM Event: " #symbol "\n"); |
| 916 | |
| 917 | do { |
| 918 | apm_event = apm_getevent(); |
| 919 | switch (apm_event) { |
| 920 | OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ); |
| 921 | if (apm_op_inprog == 0) { |
| 922 | apm_op_inprog++; |
| 923 | if (apm_record_event(sc, apm_event)) { |
| 924 | apm_suspend(PMST_STANDBY); |
| 925 | } |
| 926 | } |
| 927 | break; |
| 928 | OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ); |
| 929 | if (apm_op_inprog == 0) { |
| 930 | apm_op_inprog++; |
| 931 | if (apm_record_event(sc, apm_event)) { |
| 932 | apm_suspend(PMST_STANDBY); |
| 933 | } |
| 934 | } |
| 935 | break; |
| 936 | OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ); |
| 937 | apm_lastreq_notify(); |
| 938 | if (apm_op_inprog == 0) { |
| 939 | apm_op_inprog++; |
| 940 | if (apm_record_event(sc, apm_event)) { |
| 941 | apm_do_suspend(); |
| 942 | } |
| 943 | } |
| 944 | return; /* XXX skip the rest */ |
| 945 | OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ); |
| 946 | apm_lastreq_notify(); |
| 947 | if (apm_op_inprog == 0) { |
| 948 | apm_op_inprog++; |
| 949 | if (apm_record_event(sc, apm_event)) { |
| 950 | apm_do_suspend(); |
| 951 | } |
| 952 | } |
| 953 | return; /* XXX skip the rest */ |
| 954 | OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND); |
| 955 | apm_do_suspend(); |
| 956 | break; |
| 957 | OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME); |
| 958 | apm_record_event(sc, apm_event); |
| 959 | apm_resume(); |
| 960 | break; |
| 961 | OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME); |
| 962 | apm_record_event(sc, apm_event); |
| 963 | apm_resume(); |
| 964 | break; |
| 965 | OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME); |
| 966 | apm_record_event(sc, apm_event); |
| 967 | apm_resume(); |
| 968 | break; |
| 969 | OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW); |
| 970 | if (apm_record_event(sc, apm_event)) { |
| 971 | apm_battery_low(); |
| 972 | apm_suspend(PMST_SUSPEND); |
| 973 | } |
| 974 | break; |
| 975 | OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE); |
| 976 | apm_record_event(sc, apm_event); |
| 977 | break; |
| 978 | OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME); |
| 979 | apm_record_event(sc, apm_event); |
| 980 | inittodr(0); /* adjust time to RTC */ |
| 981 | break; |
| 982 | OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE); |
| 983 | apm_record_event(sc, apm_event); |
| 984 | break; |
| 985 | case PMEV_NOEVENT: |
| 986 | break; |
| 987 | default: |
| 988 | printf("Unknown Original APM Event 0x%x\n", apm_event); |
| 989 | break; |
| 990 | } |
| 991 | } while (apm_event != PMEV_NOEVENT); |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * Attach APM: |
| 996 | * |
| 997 | * Initialize APM driver |
| 998 | */ |
| 999 | |
| 1000 | static int |
| 1001 | apm_attach(device_t dev) |
| 1002 | { |
| 1003 | struct apm_softc *sc = &apm_softc; |
| 1004 | int flags; |
| 1005 | int drv_version; |
| 1006 | |
| 1007 | if (resource_int_value("apm", 0, "flags", &flags) != 0) |
| 1008 | flags = 0; |
| 1009 | |
| 1010 | sc->initialized = 0; |
| 1011 | |
| 1012 | /* Must be externally enabled */ |
| 1013 | sc->active = 0; |
| 1014 | |
| 1015 | /* Always call HLT in idle loop */ |
| 1016 | sc->always_halt_cpu = 1; |
| 1017 | |
| 1018 | kgetenv_int("debug.apm_debug", &apm_debug); |
| 1019 | |
| 1020 | /* print bootstrap messages */ |
| 1021 | APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version); |
| 1022 | APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n", |
| 1023 | sc->bios.seg.code16.base, sc->bios.seg.data.base); |
| 1024 | APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n", |
| 1025 | sc->bios.entry, is_enabled(sc->slow_idle_cpu), |
| 1026 | is_enabled(!sc->disabled)); |
| 1027 | APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n", |
| 1028 | sc->bios.seg.code16.limit, sc->bios.seg.data.limit); |
| 1029 | |
| 1030 | /* |
| 1031 | * In one test, apm bios version was 1.02; an attempt to register |
| 1032 | * a 1.04 driver resulted in a 1.00 connection! Registering a |
| 1033 | * 1.02 driver resulted in a 1.02 connection. |
| 1034 | */ |
| 1035 | drv_version = apm_version > 0x102 ? 0x102 : apm_version; |
| 1036 | for (; drv_version > 0x100; drv_version--) |
| 1037 | if (apm_driver_version(drv_version) == 0) |
| 1038 | break; |
| 1039 | sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 + |
| 1040 | ((drv_version & 0x000f) >> 0); |
| 1041 | sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 + |
| 1042 | ((apm_version & 0x0f00) >> 8); |
| 1043 | |
| 1044 | sc->intversion = INTVERSION(sc->majorversion, sc->minorversion); |
| 1045 | |
| 1046 | if (sc->intversion >= INTVERSION(1, 1)) |
| 1047 | APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged)); |
| 1048 | device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n", |
| 1049 | ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8), |
| 1050 | ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0), |
| 1051 | sc->majorversion, sc->minorversion); |
| 1052 | |
| 1053 | |
| 1054 | APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu)); |
| 1055 | /* enable power management */ |
| 1056 | if (sc->disabled) { |
| 1057 | if (apm_enable_disable_pm(1)) { |
| 1058 | APM_DPRINT("apm: *Warning* enable function failed! [%x]\n", |
| 1059 | (sc->bios.r.eax >> 8) & 0xff); |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | /* engage power managment (APM 1.1 or later) */ |
| 1064 | if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) { |
| 1065 | if (apm_engage_disengage_pm(1)) { |
| 1066 | APM_DPRINT("apm: *Warning* engage function failed err=[%x]", |
| 1067 | (sc->bios.r.eax >> 8) & 0xff); |
| 1068 | APM_DPRINT(" (Docked or using external power?).\n"); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | /* default suspend hook */ |
| 1073 | sc->sc_suspend.ah_fun = apm_default_suspend; |
| 1074 | sc->sc_suspend.ah_arg = sc; |
| 1075 | sc->sc_suspend.ah_name = "default suspend"; |
| 1076 | sc->sc_suspend.ah_order = APM_MAX_ORDER; |
| 1077 | |
| 1078 | /* default resume hook */ |
| 1079 | sc->sc_resume.ah_fun = apm_default_resume; |
| 1080 | sc->sc_resume.ah_arg = sc; |
| 1081 | sc->sc_resume.ah_name = "default resume"; |
| 1082 | sc->sc_resume.ah_order = APM_MIN_ORDER; |
| 1083 | |
| 1084 | apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend); |
| 1085 | apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume); |
| 1086 | |
| 1087 | /* Power the system off using APM */ |
| 1088 | EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL, |
| 1089 | SHUTDOWN_PRI_LAST); |
| 1090 | |
| 1091 | sc->initialized = 1; |
| 1092 | |
| 1093 | dev_ops_add(&apm_ops, 0, 0); |
| 1094 | make_dev(&apm_ops, 0, UID_ROOT, GID_OPERATOR, 0660, "apm"); |
| 1095 | make_dev(&apm_ops, 8, UID_ROOT, GID_OPERATOR, 0660, "apmctl"); |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | static int |
| 1100 | apmopen(struct dev_open_args *ap) |
| 1101 | { |
| 1102 | cdev_t dev = ap->a_head.a_dev; |
| 1103 | struct apm_softc *sc = &apm_softc; |
| 1104 | int ctl = APMDEV(dev); |
| 1105 | |
| 1106 | if (!sc->initialized) |
| 1107 | return (ENXIO); |
| 1108 | |
| 1109 | switch (ctl) { |
| 1110 | case APMDEV_CTL: |
| 1111 | if (!(ap->a_oflags & FWRITE)) |
| 1112 | return EINVAL; |
| 1113 | if (sc->sc_flags & SCFLAG_OCTL) |
| 1114 | return EBUSY; |
| 1115 | sc->sc_flags |= SCFLAG_OCTL; |
| 1116 | bzero(sc->event_filter, sizeof sc->event_filter); |
| 1117 | break; |
| 1118 | case APMDEV_NORMAL: |
| 1119 | sc->sc_flags |= SCFLAG_ONORMAL; |
| 1120 | break; |
| 1121 | default: |
| 1122 | return ENXIO; |
| 1123 | break; |
| 1124 | } |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | static int |
| 1129 | apmclose(struct dev_close_args *ap) |
| 1130 | { |
| 1131 | cdev_t dev = ap->a_head.a_dev; |
| 1132 | struct apm_softc *sc = &apm_softc; |
| 1133 | int ctl = APMDEV(dev); |
| 1134 | |
| 1135 | switch (ctl) { |
| 1136 | case APMDEV_CTL: |
| 1137 | apm_lastreq_rejected(); |
| 1138 | sc->sc_flags &= ~SCFLAG_OCTL; |
| 1139 | bzero(sc->event_filter, sizeof sc->event_filter); |
| 1140 | break; |
| 1141 | case APMDEV_NORMAL: |
| 1142 | sc->sc_flags &= ~SCFLAG_ONORMAL; |
| 1143 | break; |
| 1144 | } |
| 1145 | if ((sc->sc_flags & SCFLAG_OPEN) == 0) { |
| 1146 | sc->event_count = 0; |
| 1147 | sc->event_ptr = 0; |
| 1148 | } |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | static int |
| 1153 | apmioctl(struct dev_ioctl_args *ap) |
| 1154 | { |
| 1155 | cdev_t dev = ap->a_head.a_dev; |
| 1156 | struct apm_softc *sc = &apm_softc; |
| 1157 | struct apm_bios_arg *args; |
| 1158 | int error = 0; |
| 1159 | int ret; |
| 1160 | int newstate; |
| 1161 | |
| 1162 | if (!sc->initialized) |
| 1163 | return (ENXIO); |
| 1164 | APM_DPRINT("APM ioctl: cmd = 0x%lx\n", ap->a_cmd); |
| 1165 | |
| 1166 | switch (ap->a_cmd) { |
| 1167 | case APMIO_SUSPEND: |
| 1168 | if (!(ap->a_fflag & FWRITE)) |
| 1169 | return (EPERM); |
| 1170 | if (sc->active) |
| 1171 | apm_suspend(PMST_SUSPEND); |
| 1172 | else |
| 1173 | error = EINVAL; |
| 1174 | break; |
| 1175 | |
| 1176 | case APMIO_STANDBY: |
| 1177 | if (!(ap->a_fflag & FWRITE)) |
| 1178 | return (EPERM); |
| 1179 | if (sc->active) |
| 1180 | apm_suspend(PMST_STANDBY); |
| 1181 | else |
| 1182 | error = EINVAL; |
| 1183 | break; |
| 1184 | |
| 1185 | case APMIO_GETINFO_OLD: |
| 1186 | { |
| 1187 | struct apm_info info; |
| 1188 | apm_info_old_t aiop; |
| 1189 | |
| 1190 | if (apm_get_info(&info)) |
| 1191 | error = ENXIO; |
| 1192 | aiop = (apm_info_old_t)ap->a_data; |
| 1193 | aiop->ai_major = info.ai_major; |
| 1194 | aiop->ai_minor = info.ai_minor; |
| 1195 | aiop->ai_acline = info.ai_acline; |
| 1196 | aiop->ai_batt_stat = info.ai_batt_stat; |
| 1197 | aiop->ai_batt_life = info.ai_batt_life; |
| 1198 | aiop->ai_status = info.ai_status; |
| 1199 | } |
| 1200 | break; |
| 1201 | case APMIO_GETINFO: |
| 1202 | if (apm_get_info((apm_info_t)ap->a_data)) |
| 1203 | error = ENXIO; |
| 1204 | break; |
| 1205 | case APMIO_GETPWSTATUS: |
| 1206 | if (apm_get_pwstatus((apm_pwstatus_t)ap->a_data)) |
| 1207 | error = ENXIO; |
| 1208 | break; |
| 1209 | case APMIO_ENABLE: |
| 1210 | if (!(ap->a_fflag & FWRITE)) |
| 1211 | return (EPERM); |
| 1212 | apm_event_enable(); |
| 1213 | break; |
| 1214 | case APMIO_DISABLE: |
| 1215 | if (!(ap->a_fflag & FWRITE)) |
| 1216 | return (EPERM); |
| 1217 | apm_event_disable(); |
| 1218 | break; |
| 1219 | case APMIO_HALTCPU: |
| 1220 | if (!(ap->a_fflag & FWRITE)) |
| 1221 | return (EPERM); |
| 1222 | apm_halt_cpu(); |
| 1223 | break; |
| 1224 | case APMIO_NOTHALTCPU: |
| 1225 | if (!(ap->a_fflag & FWRITE)) |
| 1226 | return (EPERM); |
| 1227 | apm_not_halt_cpu(); |
| 1228 | break; |
| 1229 | case APMIO_DISPLAY: |
| 1230 | if (!(ap->a_fflag & FWRITE)) |
| 1231 | return (EPERM); |
| 1232 | newstate = *(int *)ap->a_data; |
| 1233 | if (apm_display(newstate)) |
| 1234 | error = ENXIO; |
| 1235 | break; |
| 1236 | case APMIO_BIOS: |
| 1237 | if (!(ap->a_fflag & FWRITE)) |
| 1238 | return (EPERM); |
| 1239 | /* XXX compatibility with the old interface */ |
| 1240 | args = (struct apm_bios_arg *)ap->a_data; |
| 1241 | sc->bios.r.eax = args->eax; |
| 1242 | sc->bios.r.ebx = args->ebx; |
| 1243 | sc->bios.r.ecx = args->ecx; |
| 1244 | sc->bios.r.edx = args->edx; |
| 1245 | sc->bios.r.esi = args->esi; |
| 1246 | sc->bios.r.edi = args->edi; |
| 1247 | if ((ret = apm_bioscall())) { |
| 1248 | /* |
| 1249 | * Return code 1 means bios call was unsuccessful. |
| 1250 | * Error code is stored in %ah. |
| 1251 | * Return code -1 means bios call was unsupported |
| 1252 | * in the APM BIOS version. |
| 1253 | */ |
| 1254 | if (ret == -1) { |
| 1255 | error = EINVAL; |
| 1256 | } |
| 1257 | } else { |
| 1258 | /* |
| 1259 | * Return code 0 means bios call was successful. |
| 1260 | * We need only %al and can discard %ah. |
| 1261 | */ |
| 1262 | sc->bios.r.eax &= 0xff; |
| 1263 | } |
| 1264 | args->eax = sc->bios.r.eax; |
| 1265 | args->ebx = sc->bios.r.ebx; |
| 1266 | args->ecx = sc->bios.r.ecx; |
| 1267 | args->edx = sc->bios.r.edx; |
| 1268 | args->esi = sc->bios.r.esi; |
| 1269 | args->edi = sc->bios.r.edi; |
| 1270 | break; |
| 1271 | default: |
| 1272 | error = EINVAL; |
| 1273 | break; |
| 1274 | } |
| 1275 | |
| 1276 | /* for /dev/apmctl */ |
| 1277 | if (APMDEV(dev) == APMDEV_CTL) { |
| 1278 | struct apm_event_info *evp; |
| 1279 | int i; |
| 1280 | |
| 1281 | error = 0; |
| 1282 | switch (ap->a_cmd) { |
| 1283 | case APMIO_NEXTEVENT: |
| 1284 | if (!sc->event_count) { |
| 1285 | error = EAGAIN; |
| 1286 | } else { |
| 1287 | evp = (struct apm_event_info *)ap->a_data; |
| 1288 | i = sc->event_ptr + APM_NEVENTS - sc->event_count; |
| 1289 | i %= APM_NEVENTS; |
| 1290 | *evp = sc->event_list[i]; |
| 1291 | sc->event_count--; |
| 1292 | } |
| 1293 | break; |
| 1294 | case APMIO_REJECTLASTREQ: |
| 1295 | if (apm_lastreq_rejected()) { |
| 1296 | error = EINVAL; |
| 1297 | } |
| 1298 | break; |
| 1299 | default: |
| 1300 | error = EINVAL; |
| 1301 | break; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | return error; |
| 1306 | } |
| 1307 | |
| 1308 | static int |
| 1309 | apmwrite(struct dev_write_args *ap) |
| 1310 | { |
| 1311 | cdev_t dev = ap->a_head.a_dev; |
| 1312 | struct uio *uio = ap->a_uio; |
| 1313 | struct apm_softc *sc = &apm_softc; |
| 1314 | u_int event_type; |
| 1315 | int error; |
| 1316 | u_char enabled; |
| 1317 | |
| 1318 | if (APMDEV(dev) != APMDEV_CTL) |
| 1319 | return(ENODEV); |
| 1320 | if (uio->uio_resid != sizeof(u_int)) |
| 1321 | return(E2BIG); |
| 1322 | |
| 1323 | if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio))) |
| 1324 | return(error); |
| 1325 | |
| 1326 | if (event_type < 0 || event_type >= APM_NPMEV) |
| 1327 | return(EINVAL); |
| 1328 | |
| 1329 | if (sc->event_filter[event_type] == 0) { |
| 1330 | enabled = 1; |
| 1331 | } else { |
| 1332 | enabled = 0; |
| 1333 | } |
| 1334 | sc->event_filter[event_type] = enabled; |
| 1335 | APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled)); |
| 1336 | |
| 1337 | return uio->uio_resid; |
| 1338 | } |
| 1339 | |
| 1340 | static int |
| 1341 | apmpoll(struct dev_poll_args *ap) |
| 1342 | { |
| 1343 | struct apm_softc *sc = &apm_softc; |
| 1344 | int revents = 0; |
| 1345 | |
| 1346 | if (ap->a_events & (POLLIN | POLLRDNORM)) { |
| 1347 | if (sc->event_count) { |
| 1348 | revents |= ap->a_events & (POLLIN | POLLRDNORM); |
| 1349 | } else { |
| 1350 | selrecord(curthread, &sc->sc_rsel); |
| 1351 | } |
| 1352 | } |
| 1353 | ap->a_events = revents; |
| 1354 | return (0); |
| 1355 | } |
| 1356 | |
| 1357 | /* |
| 1358 | * Because apm is a static device that always exists under any attached |
| 1359 | * isa device, and not scanned by the isa device, we need an identify |
| 1360 | * function to install the device so we can probe for it. |
| 1361 | */ |
| 1362 | static device_method_t apm_methods[] = { |
| 1363 | /* Device interface */ |
| 1364 | DEVMETHOD(device_identify, bus_generic_identify), |
| 1365 | DEVMETHOD(device_probe, apm_probe), |
| 1366 | DEVMETHOD(device_attach, apm_attach), |
| 1367 | |
| 1368 | { 0, 0 } |
| 1369 | }; |
| 1370 | |
| 1371 | static driver_t apm_driver = { |
| 1372 | "apm", |
| 1373 | apm_methods, |
| 1374 | 1, /* no softc (XXX) */ |
| 1375 | }; |
| 1376 | |
| 1377 | static devclass_t apm_devclass; |
| 1378 | |
| 1379 | DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass, 0, 0); |
| 1380 | |