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