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