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: releng/5.0/sys/pc98/apm/apm.c 105260 2002-10-16 15:44:41Z nyan $
   19  */
   20 
   21 #include <sys/param.h>
   22 #include <sys/systm.h>
   23 #include <sys/bus.h>
   24 #include <sys/conf.h>
   25 #include <sys/eventhandler.h>
   26 #include <sys/fcntl.h>
   27 #include <sys/kernel.h>
   28 #include <sys/poll.h>
   29 #include <sys/power.h>
   30 #include <sys/reboot.h>
   31 #include <sys/selinfo.h>
   32 #include <sys/signalvar.h>
   33 #include <sys/sysctl.h>
   34 #include <sys/syslog.h>
   35 #include <sys/time.h>
   36 #include <sys/uio.h>
   37 
   38 #include <machine/apm_bios.h>
   39 #include <machine/bus.h>
   40 #include <machine/clock.h>
   41 #include <machine/pc/bios.h>
   42 #include <machine/cpufunc.h>
   43 #include <machine/resource.h>
   44 #include <machine/segments.h>
   45 #include <machine/stdarg.h>
   46 #include <machine/vm86.h>
   47 
   48 #include <sys/rman.h>
   49 
   50 #include <vm/vm.h>
   51 #include <vm/pmap.h>
   52 #include <vm/vm_param.h>
   53 
   54 #include <pc98/apm/apm.h>
   55 
   56 /* Used by the apm_saver screen saver module */
   57 int apm_display(int newstate);
   58 struct apm_softc apm_softc;
   59 
   60 static void apm_resume(void);
   61 static int apm_bioscall(void);
   62 static int apm_check_function_supported(u_int version, u_int func);
   63 
   64 static int apm_pm_func(u_long, void*, ...);
   65 
   66 static u_long   apm_version;
   67 
   68 int     apm_evindex;
   69 
   70 #define SCFLAG_ONORMAL  0x0000001
   71 #define SCFLAG_OCTL     0x0000002
   72 #define SCFLAG_OPEN     (SCFLAG_ONORMAL|SCFLAG_OCTL)
   73 
   74 #define APMDEV(dev)     (minor(dev)&0x0f)
   75 #define APMDEV_NORMAL   0
   76 #define APMDEV_CTL      8
   77 
   78 #ifdef PC98
   79 extern int bios32_apm98(struct bios_regs *, u_int, u_short);
   80 
   81 /* PC98's SMM definition */
   82 #define APM_NECSMM_PORT         0x6b8e
   83 #define APM_NECSMM_PORTSZ       1
   84 #define APM_NECSMM_EN           0x10
   85 static __inline void apm_enable_smm(struct apm_softc *);
   86 static __inline void apm_disable_smm(struct apm_softc *);
   87 int apm_necsmm_addr;
   88 u_int32_t apm_necsmm_mask;
   89 #endif
   90 
   91 static struct apmhook   *hook[NAPM_HOOK];               /* XXX */
   92 
   93 #define is_enabled(foo) ((foo) ? "enabled" : "disabled")
   94 
   95 /* Map version number to integer (keeps ordering of version numbers) */
   96 #define INTVERSION(major, minor)        ((major)*100 + (minor))
   97 
   98 static struct callout_handle apm_timeout_ch = 
   99     CALLOUT_HANDLE_INITIALIZER(&apm_timeout_ch);
  100 
  101 static timeout_t apm_timeout;
  102 static d_open_t apmopen;
  103 static d_close_t apmclose;
  104 static d_write_t apmwrite;
  105 static d_ioctl_t apmioctl;
  106 static d_poll_t apmpoll;
  107 
  108 #define CDEV_MAJOR 39
  109 static struct cdevsw apm_cdevsw = {
  110         /* open */      apmopen,
  111         /* close */     apmclose,
  112         /* read */      noread,
  113         /* write */     apmwrite,
  114         /* ioctl */     apmioctl,
  115         /* poll */      apmpoll,
  116         /* mmap */      nommap,
  117         /* strategy */  nostrategy,
  118         /* name */      "apm",
  119         /* maj */       CDEV_MAJOR,
  120         /* dump */      nodump,
  121         /* psize */     nopsize,
  122         /* flags */     0,
  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(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 static 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                 halt(); /* 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 
  927         vmf.vmf_ah = APM_BIOS;
  928         vmf.vmf_al = APM_PROT32CONNECT;
  929         vmf.vmf_bx = 0;
  930         if (vm86_intcall(APM_INT, &vmf)) {
  931                 printf("apm: 32-bit connection error.\n");
  932                 return (ENXIO);
  933         }
  934 
  935         sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
  936         sc->bios.seg.code32.limit = 0xffff;
  937         sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
  938         sc->bios.seg.code16.limit = 0xffff;
  939         sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
  940         sc->bios.seg.data.limit = 0xffff;
  941         sc->bios.entry = vmf.vmf_ebx;
  942         sc->connectmode = APM_PROT32CONNECT;
  943 #else
  944         if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
  945                 vmf.vmf_ah = APM_BIOS;
  946                 vmf.vmf_al = APM_PROT32CONNECT;
  947                 vmf.vmf_bx = 0;
  948                 if (vm86_intcall(APM_INT, &vmf)) {
  949                         printf("apm: 32-bit connection error.\n");
  950                         return (ENXIO);
  951                 }
  952                 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
  953                 sc->bios.seg.code32.limit = 0xffff;
  954                 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
  955                 sc->bios.seg.code16.limit = 0xffff;
  956                 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
  957                 sc->bios.seg.data.limit = 0xffff;
  958                 sc->bios.entry = vmf.vmf_ebx;
  959                 sc->connectmode = APM_PROT32CONNECT;
  960         } else {
  961                 /* use 16-bit connection */
  962                 vmf.vmf_ah = APM_BIOS;
  963                 vmf.vmf_al = APM_PROT16CONNECT;
  964                 vmf.vmf_bx = 0;
  965                 if (vm86_intcall(APM_INT, &vmf)) {
  966                         printf("apm: 16-bit connection error.\n");
  967                         return (ENXIO);
  968                 }
  969                 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
  970                 sc->bios.seg.code16.limit = 0xffff;
  971                 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
  972                 sc->bios.seg.data.limit = 0xffff;
  973                 sc->bios.entry = vmf.vmf_bx;
  974                 sc->connectmode = APM_PROT16CONNECT;
  975         }
  976 #endif
  977         return(0);
  978 }
  979 
  980 
  981 /*
  982  * return 0 if the user will notice and handle the event,
  983  * return 1 if the kernel driver should do so.
  984  */
  985 static int
  986 apm_record_event(struct apm_softc *sc, u_int event_type)
  987 {
  988         struct apm_event_info *evp;
  989 
  990         if ((sc->sc_flags & SCFLAG_OPEN) == 0)
  991                 return 1;               /* no user waiting */
  992         if (sc->event_count == APM_NEVENTS)
  993                 return 1;                       /* overflow */
  994         if (sc->event_filter[event_type] == 0)
  995                 return 1;               /* not registered */
  996         evp = &sc->event_list[sc->event_ptr];
  997         sc->event_count++;
  998         sc->event_ptr++;
  999         sc->event_ptr %= APM_NEVENTS;
 1000         evp->type = event_type;
 1001         evp->index = ++apm_evindex;
 1002         selwakeup(&sc->sc_rsel);
 1003         return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
 1004 }
 1005 
 1006 /* Power profile */
 1007 static void
 1008 apm_power_profile(struct apm_softc *sc)
 1009 {
 1010         int state;
 1011         struct apm_info info;
 1012         static int apm_acline = 0;
 1013 
 1014         if (apm_get_info(&info))
 1015                 return;
 1016 
 1017         if (apm_acline != info.ai_acline) {
 1018                 apm_acline = info.ai_acline;
 1019                 state = apm_acline ? POWER_PROFILE_PERFORMANCE : POWER_PROFILE_ECONOMY;
 1020                 power_profile_set_state(state);
 1021         }
 1022 }
 1023 
 1024 /* Process APM event */
 1025 static void
 1026 apm_processevent(void)
 1027 {
 1028         int apm_event;
 1029         struct apm_softc *sc = &apm_softc;
 1030 
 1031 #define OPMEV_DEBUGMESSAGE(symbol) case symbol:                         \
 1032         APM_DPRINT("Received APM Event: " #symbol "\n");
 1033 
 1034         do {
 1035                 apm_event = apm_getevent();
 1036                 switch (apm_event) {
 1037                     OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
 1038                         if (apm_op_inprog == 0) {
 1039                             apm_op_inprog++;
 1040                             if (apm_record_event(sc, apm_event)) {
 1041                                 apm_suspend(PMST_STANDBY);
 1042                             }
 1043                         }
 1044                         break;
 1045                     OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
 1046                         if (apm_op_inprog == 0) {
 1047                             apm_op_inprog++;
 1048                             if (apm_record_event(sc, apm_event)) {
 1049                                 apm_suspend(PMST_STANDBY);
 1050                             }
 1051                         }
 1052                         break;
 1053                     OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
 1054                         apm_lastreq_notify();
 1055                         if (apm_op_inprog == 0) {
 1056                             apm_op_inprog++;
 1057                             if (apm_record_event(sc, apm_event)) {
 1058                                 apm_do_suspend();
 1059                             }
 1060                         }
 1061                         return; /* XXX skip the rest */
 1062                     OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
 1063                         apm_lastreq_notify();
 1064                         if (apm_op_inprog == 0) {
 1065                             apm_op_inprog++;
 1066                             if (apm_record_event(sc, apm_event)) {
 1067                                 apm_do_suspend();
 1068                             }
 1069                         }
 1070                         return; /* XXX skip the rest */
 1071                     OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
 1072                         apm_do_suspend();
 1073                         break;
 1074                     OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
 1075                         apm_record_event(sc, apm_event);
 1076                         apm_resume();
 1077                         break;
 1078                     OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
 1079                         apm_record_event(sc, apm_event);
 1080                         apm_resume();
 1081                         break;
 1082                     OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
 1083                         apm_record_event(sc, apm_event);
 1084                         break;
 1085                     OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
 1086                         if (apm_record_event(sc, apm_event)) {
 1087                             apm_battery_low();
 1088                             apm_suspend(PMST_SUSPEND);
 1089                         }
 1090                         break;
 1091                     OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
 1092                         apm_record_event(sc, apm_event);
 1093                         apm_power_profile(sc);
 1094                         break;
 1095                     OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
 1096                         apm_record_event(sc, apm_event);
 1097                         inittodr(0);    /* adjust time to RTC */
 1098                         break;
 1099                     OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
 1100                         apm_record_event(sc, apm_event);
 1101                         apm_power_profile(sc);
 1102                         break;
 1103                     case PMEV_NOEVENT:
 1104                         break;
 1105                     default:
 1106                         printf("Unknown Original APM Event 0x%x\n", apm_event);
 1107                             break;
 1108                 }
 1109         } while (apm_event != PMEV_NOEVENT);
 1110 #ifdef PC98
 1111         apm_disable_smm(sc);
 1112 #endif
 1113 }
 1114 
 1115 /*
 1116  * Attach APM:
 1117  *
 1118  * Initialize APM driver
 1119  */
 1120 
 1121 static int
 1122 apm_attach(device_t dev)
 1123 {
 1124         struct apm_softc        *sc = &apm_softc;
 1125         int                     flags;
 1126         int                     drv_version;
 1127 #ifdef PC98
 1128         int                     rid;
 1129 #endif
 1130 
 1131         if (resource_int_value("apm", 0, "flags", &flags) != 0)
 1132                 flags = 0;
 1133 
 1134         if (flags & 0x20)
 1135                 statclock_disable = 1;
 1136 
 1137         sc->initialized = 0;
 1138 
 1139         /* Must be externally enabled */
 1140         sc->active = 0;
 1141 
 1142         /* Always call HLT in idle loop */
 1143         sc->always_halt_cpu = 1;
 1144 
 1145         getenv_int("debug.apm_debug", &apm_debug);
 1146 
 1147         /* print bootstrap messages */
 1148         APM_DPRINT("apm: APM BIOS version %04lx\n",  apm_version);
 1149         APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
 1150             sc->bios.seg.code16.base, sc->bios.seg.data.base);
 1151         APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
 1152             sc->bios.entry, is_enabled(sc->slow_idle_cpu),
 1153             is_enabled(!sc->disabled));
 1154         APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
 1155             sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
 1156 
 1157 #ifdef PC98
 1158         rid = 0;
 1159         sc->sc_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
 1160                          APM_NECSMM_PORT, ~0, APM_NECSMM_PORTSZ, RF_ACTIVE);
 1161         if (sc->sc_res == NULL)
 1162                 panic("%s: counldn't map I/O ports", device_get_name(dev));
 1163         sc->sc_iot = rman_get_bustag(sc->sc_res);
 1164         sc->sc_ioh = rman_get_bushandle(sc->sc_res);
 1165 
 1166         if (apm_version==0x112 || apm_version==0x111 || apm_version==0x110)
 1167                 apm_necsmm_addr = APM_NECSMM_PORT;
 1168         else
 1169                 apm_necsmm_addr = 0;
 1170         apm_necsmm_mask = ~APM_NECSMM_EN;
 1171 #endif /* PC98 */
 1172 
 1173         /*
 1174          * In one test, apm bios version was 1.02; an attempt to register
 1175          * a 1.04 driver resulted in a 1.00 connection!  Registering a
 1176          * 1.02 driver resulted in a 1.02 connection.
 1177          */
 1178         drv_version = apm_version > 0x102 ? 0x102 : apm_version;
 1179         for (; drv_version > 0x100; drv_version--)
 1180                 if (apm_driver_version(drv_version) == 0)
 1181                         break;
 1182         sc->minorversion = ((drv_version & 0x00f0) >>  4) * 10 +
 1183                 ((drv_version & 0x000f) >> 0);
 1184         sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
 1185                 ((apm_version & 0x0f00) >> 8);
 1186 
 1187         sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
 1188 
 1189         if (sc->intversion >= INTVERSION(1, 1))
 1190                 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
 1191         device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
 1192                ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
 1193                ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
 1194                sc->majorversion, sc->minorversion);
 1195 
 1196 
 1197         APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
 1198         /* enable power management */
 1199         if (sc->disabled) {
 1200                 if (apm_enable_disable_pm(1)) {
 1201                         APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
 1202                             (sc->bios.r.eax >> 8) & 0xff);
 1203                 }
 1204         }
 1205 
 1206         /* engage power managment (APM 1.1 or later) */
 1207         if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
 1208                 if (apm_engage_disengage_pm(1)) {
 1209                         APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
 1210                             (sc->bios.r.eax >> 8) & 0xff);
 1211                         APM_DPRINT(" (Docked or using external power?).\n");
 1212                 }
 1213         }
 1214 
 1215         /* Power the system off using APM */
 1216         EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL, 
 1217                               SHUTDOWN_PRI_LAST);
 1218 
 1219         /* Register APM again to pass the correct argument of pm_func. */
 1220         power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, sc);
 1221 
 1222         sc->initialized = 1;
 1223         sc->suspending = 0;
 1224 
 1225         make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm");
 1226         make_dev(&apm_cdevsw, 8, 0, 5, 0660, "apmctl");
 1227         return 0;
 1228 }
 1229 
 1230 static int
 1231 apmopen(dev_t dev, int flag, int fmt, struct thread *td)
 1232 {
 1233         struct apm_softc *sc = &apm_softc;
 1234         int ctl = APMDEV(dev);
 1235 
 1236         if (!sc->initialized)
 1237                 return (ENXIO);
 1238 
 1239         switch (ctl) {
 1240         case APMDEV_CTL:
 1241                 if (!(flag & FWRITE))
 1242                         return EINVAL;
 1243                 if (sc->sc_flags & SCFLAG_OCTL)
 1244                         return EBUSY;
 1245                 sc->sc_flags |= SCFLAG_OCTL;
 1246                 bzero(sc->event_filter, sizeof sc->event_filter);
 1247                 break;
 1248         case APMDEV_NORMAL:
 1249                 sc->sc_flags |= SCFLAG_ONORMAL;
 1250                 break;
 1251         default:
 1252                 return ENXIO;
 1253                 break;
 1254         }
 1255         return 0;
 1256 }
 1257 
 1258 static int
 1259 apmclose(dev_t dev, int flag, int fmt, struct thread *td)
 1260 {
 1261         struct apm_softc *sc = &apm_softc;
 1262         int ctl = APMDEV(dev);
 1263 
 1264         switch (ctl) {
 1265         case APMDEV_CTL:
 1266                 apm_lastreq_rejected();
 1267                 sc->sc_flags &= ~SCFLAG_OCTL;
 1268                 bzero(sc->event_filter, sizeof sc->event_filter);
 1269                 break;
 1270         case APMDEV_NORMAL:
 1271                 sc->sc_flags &= ~SCFLAG_ONORMAL;
 1272                 break;
 1273         }
 1274         if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
 1275                 sc->event_count = 0;
 1276                 sc->event_ptr = 0;
 1277         }
 1278         return 0;
 1279 }
 1280 
 1281 static int
 1282 apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
 1283 {
 1284         struct apm_softc *sc = &apm_softc;
 1285         struct apm_bios_arg *args;
 1286         int error = 0;
 1287         int ret;
 1288         int newstate;
 1289 
 1290         if (!sc->initialized)
 1291                 return (ENXIO);
 1292         APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd);
 1293         switch (cmd) {
 1294         case APMIO_SUSPEND:
 1295                 if (!(flag & FWRITE))
 1296                         return (EPERM);
 1297                 if (sc->active)
 1298                         apm_suspend(PMST_SUSPEND);
 1299                 else
 1300                         error = EINVAL;
 1301                 break;
 1302 
 1303         case APMIO_STANDBY:
 1304                 if (!(flag & FWRITE))
 1305                         return (EPERM);
 1306                 if (sc->active)
 1307                         apm_suspend(PMST_STANDBY);
 1308                 else
 1309                         error = EINVAL;
 1310                 break;
 1311 
 1312         case APMIO_GETINFO_OLD:
 1313                 {
 1314                         struct apm_info info;
 1315                         apm_info_old_t aiop;
 1316 
 1317                         if (apm_get_info(&info))
 1318                                 error = ENXIO;
 1319                         aiop = (apm_info_old_t)addr;
 1320                         aiop->ai_major = info.ai_major;
 1321                         aiop->ai_minor = info.ai_minor;
 1322                         aiop->ai_acline = info.ai_acline;
 1323                         aiop->ai_batt_stat = info.ai_batt_stat;
 1324                         aiop->ai_batt_life = info.ai_batt_life;
 1325                         aiop->ai_status = info.ai_status;
 1326                 }
 1327                 break;
 1328         case APMIO_GETINFO:
 1329                 if (apm_get_info((apm_info_t)addr))
 1330                         error = ENXIO;
 1331                 break;
 1332         case APMIO_GETPWSTATUS:
 1333                 if (apm_get_pwstatus((apm_pwstatus_t)addr))
 1334                         error = ENXIO;
 1335                 break;
 1336         case APMIO_ENABLE:
 1337                 if (!(flag & FWRITE))
 1338                         return (EPERM);
 1339                 apm_event_enable();
 1340                 break;
 1341         case APMIO_DISABLE:
 1342                 if (!(flag & FWRITE))
 1343                         return (EPERM);
 1344                 apm_event_disable();
 1345                 break;
 1346         case APMIO_HALTCPU:
 1347                 if (!(flag & FWRITE))
 1348                         return (EPERM);
 1349                 apm_halt_cpu();
 1350                 break;
 1351         case APMIO_NOTHALTCPU:
 1352                 if (!(flag & FWRITE))
 1353                         return (EPERM);
 1354                 apm_not_halt_cpu();
 1355                 break;
 1356         case APMIO_DISPLAY:
 1357                 if (!(flag & FWRITE))
 1358                         return (EPERM);
 1359                 newstate = *(int *)addr;
 1360                 if (apm_display(newstate))
 1361                         error = ENXIO;
 1362                 break;
 1363         case APMIO_BIOS:
 1364                 if (!(flag & FWRITE))
 1365                         return (EPERM);
 1366                 /* XXX compatibility with the old interface */
 1367                 args = (struct apm_bios_arg *)addr;
 1368                 sc->bios.r.eax = args->eax;
 1369                 sc->bios.r.ebx = args->ebx;
 1370                 sc->bios.r.ecx = args->ecx;
 1371                 sc->bios.r.edx = args->edx;
 1372                 sc->bios.r.esi = args->esi;
 1373                 sc->bios.r.edi = args->edi;
 1374                 if ((ret = apm_bioscall())) {
 1375                         /*
 1376                          * Return code 1 means bios call was unsuccessful.
 1377                          * Error code is stored in %ah.
 1378                          * Return code -1 means bios call was unsupported
 1379                          * in the APM BIOS version.
 1380                          */
 1381                         if (ret == -1) {
 1382                                 error = EINVAL;
 1383                         }
 1384                 } else {
 1385                         /*
 1386                          * Return code 0 means bios call was successful.
 1387                          * We need only %al and can discard %ah.
 1388                          */
 1389                         sc->bios.r.eax &= 0xff;
 1390                 }
 1391                 args->eax = sc->bios.r.eax;
 1392                 args->ebx = sc->bios.r.ebx;
 1393                 args->ecx = sc->bios.r.ecx;
 1394                 args->edx = sc->bios.r.edx;
 1395                 args->esi = sc->bios.r.esi;
 1396                 args->edi = sc->bios.r.edi;
 1397                 break;
 1398         default:
 1399                 error = EINVAL;
 1400                 break;
 1401         }
 1402 
 1403         /* for /dev/apmctl */
 1404         if (APMDEV(dev) == APMDEV_CTL) {
 1405                 struct apm_event_info *evp;
 1406                 int i;
 1407 
 1408                 error = 0;
 1409                 switch (cmd) {
 1410                 case APMIO_NEXTEVENT:
 1411                         if (!sc->event_count) {
 1412                                 error = EAGAIN;
 1413                         } else {
 1414                                 evp = (struct apm_event_info *)addr;
 1415                                 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
 1416                                 i %= APM_NEVENTS;
 1417                                 *evp = sc->event_list[i];
 1418                                 sc->event_count--;
 1419                         }
 1420                         break;
 1421                 case APMIO_REJECTLASTREQ:
 1422                         if (apm_lastreq_rejected()) {
 1423                                 error = EINVAL;
 1424                         }
 1425                         break;
 1426                 default:
 1427                         error = EINVAL;
 1428                         break;
 1429                 }
 1430         }
 1431 
 1432         return error;
 1433 }
 1434 
 1435 static int
 1436 apmwrite(dev_t dev, struct uio *uio, int ioflag)
 1437 {
 1438         struct apm_softc *sc = &apm_softc;
 1439         u_int event_type;
 1440         int error;
 1441         u_char enabled;
 1442 
 1443         if (APMDEV(dev) != APMDEV_CTL)
 1444                 return(ENODEV);
 1445         if (uio->uio_resid != sizeof(u_int))
 1446                 return(E2BIG);
 1447 
 1448         if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
 1449                 return(error);
 1450 
 1451         if (event_type < 0 || event_type >= APM_NPMEV)
 1452                 return(EINVAL);
 1453 
 1454         if (sc->event_filter[event_type] == 0) {
 1455                 enabled = 1;
 1456         } else {
 1457                 enabled = 0;
 1458         }
 1459         sc->event_filter[event_type] = enabled;
 1460         APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
 1461 
 1462         return uio->uio_resid;
 1463 }
 1464 
 1465 static int
 1466 apmpoll(dev_t dev, int events, struct thread *td)
 1467 {
 1468         struct apm_softc *sc = &apm_softc;
 1469         int revents = 0;
 1470 
 1471         if (events & (POLLIN | POLLRDNORM)) {
 1472                 if (sc->event_count) {
 1473                         revents |= events & (POLLIN | POLLRDNORM);
 1474                 } else {
 1475                         selrecord(td, &sc->sc_rsel);
 1476                 }
 1477         }
 1478 
 1479         return (revents);
 1480 }
 1481 
 1482 static device_method_t apm_methods[] = {
 1483         /* Device interface */
 1484         DEVMETHOD(device_identify,      apm_identify),
 1485         DEVMETHOD(device_probe,         apm_probe),
 1486         DEVMETHOD(device_attach,        apm_attach),
 1487 
 1488         { 0, 0 }
 1489 };
 1490 
 1491 static driver_t apm_driver = {
 1492         "apm",
 1493         apm_methods,
 1494         1,                      /* no softc (XXX) */
 1495 };
 1496 
 1497 static devclass_t apm_devclass;
 1498 
 1499 DRIVER_MODULE(apm, legacy, apm_driver, apm_devclass, apm_modevent, 0);
 1500 MODULE_VERSION(apm, 1);
 1501 
 1502 static int
 1503 apm_pm_func(u_long cmd, void *arg, ...)
 1504 {
 1505         int     state, apm_state;
 1506         int     error;
 1507         va_list ap;
 1508 
 1509         error = 0;
 1510         switch (cmd) {
 1511         case POWER_CMD_SUSPEND:
 1512                 va_start(ap, arg);
 1513                 state = va_arg(ap, int);
 1514                 va_end(ap);     
 1515 
 1516                 switch (state) {
 1517                 case POWER_SLEEP_STATE_STANDBY:
 1518                         apm_state = PMST_STANDBY;
 1519                         break;
 1520                 case POWER_SLEEP_STATE_SUSPEND:
 1521                 case POWER_SLEEP_STATE_HIBERNATE:
 1522                         apm_state = PMST_SUSPEND;
 1523                         break;
 1524                 default:
 1525                         error = EINVAL;
 1526                         goto out;
 1527                 }
 1528 
 1529                 apm_suspend(apm_state);
 1530                 break;
 1531 
 1532         default:
 1533                 error = EINVAL;
 1534                 goto out;
 1535         }
 1536 
 1537 out:
 1538         return (error);
 1539 }
 1540 
 1541 static void
 1542 apm_pm_register(void *arg)
 1543 {
 1544         int     disabled = 0;
 1545 
 1546         resource_int_value("apm", 0, "disabled", &disabled);
 1547         if (disabled == 0)
 1548                 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, NULL);
 1549 }
 1550 
 1551 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, apm_pm_register, 0);

Cache object: 94bbc9a80864b0507cad74a65db7f39b


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