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

Cache object: 66915df4815d85e63f73101f288d0f9b


[ 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.