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

Cache object: a45c7cb3066bd0f95afe4d67751b4a39


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