The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/i386/bios/apm.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 989e94d572ef6c83bb6ccf508c74dfbc


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.