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