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/pc98/apm/apm.c

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

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

Cache object: 2b8f2b79ce603004f8a20c85f2f6b191


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