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