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