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