FreeBSD/Linux Kernel Cross Reference
sys/i386/bios/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
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD: releng/5.2/sys/i386/bios/apm.c 122352 2003-11-09 09:17:26Z tanimura $");
21
22 #include <sys/param.h>
23 #include <sys/systm.h>
24 #include <sys/bus.h>
25 #include <sys/conf.h>
26 #include <sys/eventhandler.h>
27 #include <sys/fcntl.h>
28 #include <sys/kernel.h>
29 #include <sys/poll.h>
30 #include <sys/power.h>
31 #include <sys/reboot.h>
32 #include <sys/selinfo.h>
33 #include <sys/signalvar.h>
34 #include <sys/sysctl.h>
35 #include <sys/syslog.h>
36 #include <sys/time.h>
37 #include <sys/uio.h>
38
39 #include <machine/apm_bios.h>
40 #include <machine/clock.h>
41 #include <machine/endian.h>
42 #include <machine/pc/bios.h>
43 #include <machine/cpufunc.h>
44 #include <machine/segments.h>
45 #include <machine/stdarg.h>
46 #include <machine/vm86.h>
47
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <sys/rman.h>
51
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 #include <vm/vm_param.h>
55
56 #include <i386/bios/apm.h>
57
58 /* Used by the apm_saver screen saver module */
59 int apm_display(int newstate);
60 struct apm_softc apm_softc;
61
62 static void apm_resume(void);
63 static int apm_bioscall(void);
64 static int apm_check_function_supported(u_int version, u_int func);
65
66 static int apm_pm_func(u_long, void*, ...);
67
68 static u_long apm_version;
69
70 int apm_evindex;
71
72 #define SCFLAG_ONORMAL 0x0000001
73 #define SCFLAG_OCTL 0x0000002
74 #define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL)
75
76 #define APMDEV(dev) (minor(dev)&0x0f)
77 #define APMDEV_NORMAL 0
78 #define APMDEV_CTL 8
79
80 #ifdef PC98
81 extern int bios32_apm98(struct bios_regs *, u_int, u_short);
82
83 /* PC98's SMM definition */
84 #define APM_NECSMM_PORT 0x6b8e
85 #define APM_NECSMM_PORTSZ 1
86 #define APM_NECSMM_EN 0x10
87 static __inline void apm_enable_smm(struct apm_softc *);
88 static __inline void apm_disable_smm(struct apm_softc *);
89 int apm_necsmm_addr;
90 u_int32_t apm_necsmm_mask;
91 #endif
92
93 static struct apmhook *hook[NAPM_HOOK]; /* XXX */
94
95 #define is_enabled(foo) ((foo) ? "enabled" : "disabled")
96
97 /* Map version number to integer (keeps ordering of version numbers) */
98 #define INTVERSION(major, minor) ((major)*100 + (minor))
99
100 static struct callout_handle apm_timeout_ch =
101 CALLOUT_HANDLE_INITIALIZER(&apm_timeout_ch);
102
103 static timeout_t apm_timeout;
104 static d_open_t apmopen;
105 static d_close_t apmclose;
106 static d_write_t apmwrite;
107 static d_ioctl_t apmioctl;
108 static d_poll_t apmpoll;
109
110 #define CDEV_MAJOR 39
111 static struct cdevsw apm_cdevsw = {
112 .d_open = apmopen,
113 .d_close = apmclose,
114 .d_write = apmwrite,
115 .d_ioctl = apmioctl,
116 .d_poll = apmpoll,
117 .d_name = "apm",
118 .d_maj = CDEV_MAJOR,
119 };
120
121 static int apm_suspend_delay = 1;
122 static int apm_standby_delay = 1;
123 static int apm_swab_batt_minutes = 0;
124 static int apm_debug = 0;
125
126 #define APM_DPRINT(args...) do { \
127 if (apm_debug) { \
128 printf(args); \
129 } \
130 } while (0)
131
132 SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
133 SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
134 SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
135
136 TUNABLE_INT("machdep.apm_swab_batt_minutes", &apm_swab_batt_minutes);
137 SYSCTL_INT(_machdep, OID_AUTO, apm_swab_batt_minutes, CTLFLAG_RW,
138 &apm_swab_batt_minutes, 0, "Byte swap battery time value.");
139
140 #ifdef PC98
141 static __inline void
142 apm_enable_smm(sc)
143 struct apm_softc *sc;
144 {
145 bus_space_tag_t iot = sc->sc_iot;
146 bus_space_handle_t ioh = sc->sc_ioh;
147 if (apm_necsmm_addr != 0)
148 bus_space_write_1(iot, ioh, 0,
149 (bus_space_read_1(iot, ioh, 0) | ~apm_necsmm_mask));
150 }
151
152 static __inline void
153 apm_disable_smm(sc)
154 struct apm_softc *sc;
155 {
156 bus_space_tag_t iot = sc->sc_iot;
157 bus_space_handle_t ioh = sc->sc_ioh;
158 if (apm_necsmm_addr != 0)
159 bus_space_write_1(iot, ioh, 0,
160 (bus_space_read_1(iot, ioh, 0) & apm_necsmm_mask));
161 }
162 #endif
163
164 /*
165 * return 0 if the function successfull,
166 * return 1 if the function unsuccessfull,
167 * return -1 if the function unsupported.
168 */
169 static int
170 apm_bioscall(void)
171 {
172 struct apm_softc *sc = &apm_softc;
173 int errno = 0;
174 u_int apm_func = sc->bios.r.eax & 0xff;
175
176 if (!apm_check_function_supported(sc->intversion, apm_func)) {
177 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
178 apm_func, sc->majorversion, sc->minorversion);
179 return (-1);
180 }
181
182 sc->bios_busy = 1;
183 #ifdef PC98
184 set_bios_selectors(&sc->bios.seg, BIOSCODE_FLAG | BIOSDATA_FLAG);
185 if (bios32_apm98(&sc->bios.r, sc->bios.entry,
186 GSEL(GBIOSCODE32_SEL, SEL_KPL)) != 0)
187 return 1;
188 #else
189 if (sc->connectmode == APM_PROT32CONNECT) {
190 set_bios_selectors(&sc->bios.seg,
191 BIOSCODE_FLAG | BIOSDATA_FLAG);
192 errno = bios32(&sc->bios.r,
193 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
194 } else {
195 errno = bios16(&sc->bios, NULL);
196 }
197 #endif
198 sc->bios_busy = 0;
199 return (errno);
200 }
201
202 /* check whether APM function is supported (1) or not (0). */
203 static int
204 apm_check_function_supported(u_int version, u_int func)
205 {
206 /* except driver version */
207 if (func == APM_DRVVERSION) {
208 return (1);
209 }
210 #ifdef PC98
211 if (func == APM_GETPWSTATUS) {
212 return (1);
213 }
214 #endif
215
216 switch (version) {
217 case INTVERSION(1, 0):
218 if (func > APM_GETPMEVENT) {
219 return (0); /* not supported */
220 }
221 break;
222 case INTVERSION(1, 1):
223 if (func > APM_ENGAGEDISENGAGEPM &&
224 func < APM_OEMFUNC) {
225 return (0); /* not supported */
226 }
227 break;
228 case INTVERSION(1, 2):
229 break;
230 }
231
232 return (1); /* supported */
233 }
234
235 /* enable/disable power management */
236 static int
237 apm_enable_disable_pm(int enable)
238 {
239 struct apm_softc *sc = &apm_softc;
240
241 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
242
243 if (sc->intversion >= INTVERSION(1, 1))
244 sc->bios.r.ebx = PMDV_ALLDEV;
245 else
246 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
247 sc->bios.r.ecx = enable;
248 sc->bios.r.edx = 0;
249 return (apm_bioscall());
250 }
251
252 /* register driver version (APM 1.1 or later) */
253 static int
254 apm_driver_version(int version)
255 {
256 struct apm_softc *sc = &apm_softc;
257
258 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
259 sc->bios.r.ebx = 0x0;
260 sc->bios.r.ecx = version;
261 sc->bios.r.edx = 0;
262
263 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
264 return (0);
265
266 /* Some old BIOSes don't return the connection version in %ax. */
267 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
268 return (0);
269
270 return (1);
271 }
272
273 /* engage/disengage power management (APM 1.1 or later) */
274 static int
275 apm_engage_disengage_pm(int engage)
276 {
277 struct apm_softc *sc = &apm_softc;
278
279 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
280 sc->bios.r.ebx = PMDV_ALLDEV;
281 sc->bios.r.ecx = engage;
282 sc->bios.r.edx = 0;
283 return (apm_bioscall());
284 }
285
286 /* get PM event */
287 static u_int
288 apm_getevent(void)
289 {
290 struct apm_softc *sc = &apm_softc;
291
292 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
293
294 sc->bios.r.ebx = 0;
295 sc->bios.r.ecx = 0;
296 sc->bios.r.edx = 0;
297 if (apm_bioscall())
298 return (PMEV_NOEVENT);
299 return (sc->bios.r.ebx & 0xffff);
300 }
301
302 /* suspend entire system */
303 static int
304 apm_suspend_system(int state)
305 {
306 struct apm_softc *sc = &apm_softc;
307
308 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
309 sc->bios.r.ebx = PMDV_ALLDEV;
310 sc->bios.r.ecx = state;
311 sc->bios.r.edx = 0;
312
313 #ifdef PC98
314 apm_disable_smm(sc);
315 #endif
316 if (apm_bioscall()) {
317 printf("Entire system suspend failure: errcode = %d\n",
318 0xff & (sc->bios.r.eax >> 8));
319 return 1;
320 }
321 #ifdef PC98
322 apm_enable_smm(sc);
323 #endif
324 return 0;
325 }
326
327 /* Display control */
328 /*
329 * Experimental implementation: My laptop machine can't handle this function
330 * If your laptop can control the display via APM, please inform me.
331 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
332 */
333 int
334 apm_display(int newstate)
335 {
336 struct apm_softc *sc = &apm_softc;
337
338 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
339 sc->bios.r.ebx = PMDV_DISP0;
340 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
341 sc->bios.r.edx = 0;
342 if (apm_bioscall() == 0) {
343 return 0;
344 }
345
346 /* If failed, then try to blank all display devices instead. */
347 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
348 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
349 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
350 sc->bios.r.edx = 0;
351 if (apm_bioscall() == 0) {
352 return 0;
353 }
354 printf("Display off failure: errcode = %d\n",
355 0xff & (sc->bios.r.eax >> 8));
356 return 1;
357 }
358
359 /*
360 * Turn off the entire system.
361 */
362 static void
363 apm_power_off(void *junk, int howto)
364 {
365 struct apm_softc *sc = &apm_softc;
366
367 /* Not halting powering off, or not active */
368 if (!(howto & RB_POWEROFF) || !apm_softc.active)
369 return;
370 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
371 sc->bios.r.ebx = PMDV_ALLDEV;
372 sc->bios.r.ecx = PMST_OFF;
373 sc->bios.r.edx = 0;
374 (void) apm_bioscall();
375 }
376
377 /* APM Battery low handler */
378 static void
379 apm_battery_low(void)
380 {
381 printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
382 }
383
384 /* APM hook manager */
385 static struct apmhook *
386 apm_add_hook(struct apmhook **list, struct apmhook *ah)
387 {
388 int s;
389 struct apmhook *p, *prev;
390
391 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
392
393 s = splhigh();
394 if (ah == NULL)
395 panic("illegal apm_hook!");
396 prev = NULL;
397 for (p = *list; p != NULL; prev = p, p = p->ah_next)
398 if (p->ah_order > ah->ah_order)
399 break;
400
401 if (prev == NULL) {
402 ah->ah_next = *list;
403 *list = ah;
404 } else {
405 ah->ah_next = prev->ah_next;
406 prev->ah_next = ah;
407 }
408 splx(s);
409 return ah;
410 }
411
412 static void
413 apm_del_hook(struct apmhook **list, struct apmhook *ah)
414 {
415 int s;
416 struct apmhook *p, *prev;
417
418 s = splhigh();
419 prev = NULL;
420 for (p = *list; p != NULL; prev = p, p = p->ah_next)
421 if (p == ah)
422 goto deleteit;
423 panic("Tried to delete unregistered apm_hook.");
424 goto nosuchnode;
425 deleteit:
426 if (prev != NULL)
427 prev->ah_next = p->ah_next;
428 else
429 *list = p->ah_next;
430 nosuchnode:
431 splx(s);
432 }
433
434
435 /* APM driver calls some functions automatically */
436 static void
437 apm_execute_hook(struct apmhook *list)
438 {
439 struct apmhook *p;
440
441 for (p = list; p != NULL; p = p->ah_next) {
442 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
443 if ((*(p->ah_fun))(p->ah_arg))
444 printf("Warning: APM hook \"%s\" failed", p->ah_name);
445 }
446 }
447
448
449 /* establish an apm hook */
450 struct apmhook *
451 apm_hook_establish(int apmh, struct apmhook *ah)
452 {
453 if (apmh < 0 || apmh >= NAPM_HOOK)
454 return NULL;
455
456 return apm_add_hook(&hook[apmh], ah);
457 }
458
459 /* disestablish an apm hook */
460 void
461 apm_hook_disestablish(int apmh, struct apmhook *ah)
462 {
463 if (apmh < 0 || apmh >= NAPM_HOOK)
464 return;
465
466 apm_del_hook(&hook[apmh], ah);
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 sc->suspending = 1;
494 apm_processevent();
495 } else {
496 /* Failure, 'resume' the system again */
497 apm_execute_hook(hook[APM_HOOK_RESUME]);
498 DEVICE_RESUME(root_bus);
499 }
500 }
501 }
502 }
503
504 static void
505 apm_do_standby(void)
506 {
507 struct apm_softc *sc = &apm_softc;
508
509 if (!sc)
510 return;
511
512 apm_op_inprog = 0;
513 sc->standbys = sc->standby_countdown = 0;
514
515 if (sc->initialized) {
516 /*
517 * As far as standby, we don't need to execute
518 * all of suspend hooks.
519 */
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 static void
597 apm_resume(void)
598 {
599 struct apm_softc *sc = &apm_softc;
600
601 if (!sc)
602 return;
603
604 if (sc->suspending == 0)
605 return;
606
607 sc->suspending = 0;
608 if (sc->initialized) {
609 apm_execute_hook(hook[APM_HOOK_RESUME]);
610 DEVICE_RESUME(root_bus);
611 }
612 }
613
614
615 /* get power status per battery */
616 static int
617 apm_get_pwstatus(apm_pwstatus_t app)
618 {
619 struct apm_softc *sc = &apm_softc;
620
621 if (app->ap_device != PMDV_ALLDEV &&
622 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
623 return 1;
624
625 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
626 sc->bios.r.ebx = app->ap_device;
627 sc->bios.r.ecx = 0;
628 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
629
630 if (apm_bioscall())
631 return 1;
632
633 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
634 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
635 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
636 app->ap_batt_life = sc->bios.r.ecx & 0xff;
637 sc->bios.r.edx &= 0xffff;
638 if (apm_swab_batt_minutes)
639 sc->bios.r.edx = __bswap16(sc->bios.r.edx) | 0x8000;
640 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
641 app->ap_batt_time = -1;
642 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
643 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
644 else /* Time is in seconds */
645 app->ap_batt_time = sc->bios.r.edx;
646
647 return 0;
648 }
649
650
651 /* get APM information */
652 static int
653 apm_get_info(apm_info_t aip)
654 {
655 struct apm_softc *sc = &apm_softc;
656 struct apm_pwstatus aps;
657
658 bzero(&aps, sizeof(aps));
659 aps.ap_device = PMDV_ALLDEV;
660 if (apm_get_pwstatus(&aps))
661 return 1;
662
663 aip->ai_infoversion = 1;
664 aip->ai_acline = aps.ap_acline;
665 aip->ai_batt_stat = aps.ap_batt_stat;
666 aip->ai_batt_life = aps.ap_batt_life;
667 aip->ai_batt_time = aps.ap_batt_time;
668 aip->ai_major = (u_int)sc->majorversion;
669 aip->ai_minor = (u_int)sc->minorversion;
670 aip->ai_status = (u_int)sc->active;
671
672 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
673 sc->bios.r.ebx = 0;
674 sc->bios.r.ecx = 0;
675 sc->bios.r.edx = 0;
676 if (apm_bioscall()) {
677 aip->ai_batteries = -1; /* Unknown */
678 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
679 } else {
680 aip->ai_batteries = sc->bios.r.ebx & 0xff;
681 aip->ai_capabilities = sc->bios.r.ecx & 0xf;
682 }
683
684 bzero(aip->ai_spare, sizeof aip->ai_spare);
685
686 return 0;
687 }
688
689
690 /* inform APM BIOS that CPU is idle */
691 void
692 apm_cpu_idle(void)
693 {
694 struct apm_softc *sc = &apm_softc;
695
696 if (sc->active) {
697
698 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
699 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
700 (void) apm_bioscall();
701 }
702 /*
703 * Some APM implementation halts CPU in BIOS, whenever
704 * "CPU-idle" function are invoked, but swtch() of
705 * FreeBSD halts CPU, therefore, CPU is halted twice
706 * in the sched loop. It makes the interrupt latency
707 * terribly long and be able to cause a serious problem
708 * in interrupt processing. We prevent it by removing
709 * "hlt" operation from swtch() and managed it under
710 * APM driver.
711 */
712 if (!sc->active || sc->always_halt_cpu)
713 halt(); /* wait for interrupt */
714 }
715
716 /* inform APM BIOS that CPU is busy */
717 void
718 apm_cpu_busy(void)
719 {
720 struct apm_softc *sc = &apm_softc;
721
722 /*
723 * The APM specification says this is only necessary if your BIOS
724 * slows down the processor in the idle task, otherwise it's not
725 * necessary.
726 */
727 if (sc->slow_idle_cpu && sc->active) {
728
729 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
730 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
731 apm_bioscall();
732 }
733 }
734
735
736 /*
737 * APM timeout routine:
738 *
739 * This routine is automatically called by timer once per second.
740 */
741
742 static void
743 apm_timeout(void *dummy)
744 {
745 struct apm_softc *sc = &apm_softc;
746
747 if (apm_op_inprog)
748 apm_lastreq_notify();
749
750 if (sc->standbys && sc->standby_countdown-- <= 0)
751 apm_do_standby();
752
753 if (sc->suspends && sc->suspend_countdown-- <= 0)
754 apm_do_suspend();
755
756 if (!sc->bios_busy)
757 apm_processevent();
758
759 if (sc->active == 1)
760 /* Run slightly more oftan than 1 Hz */
761 apm_timeout_ch = timeout(apm_timeout, NULL, hz - 1);
762 }
763
764 /* enable APM BIOS */
765 static void
766 apm_event_enable(void)
767 {
768 struct apm_softc *sc = &apm_softc;
769
770 APM_DPRINT("called apm_event_enable()\n");
771 if (sc->initialized) {
772 sc->active = 1;
773 apm_timeout(sc);
774 }
775 }
776
777 /* disable APM BIOS */
778 static void
779 apm_event_disable(void)
780 {
781 struct apm_softc *sc = &apm_softc;
782
783 APM_DPRINT("called apm_event_disable()\n");
784 if (sc->initialized) {
785 untimeout(apm_timeout, NULL, apm_timeout_ch);
786 sc->active = 0;
787 }
788 }
789
790 /* halt CPU in scheduling loop */
791 static void
792 apm_halt_cpu(void)
793 {
794 struct apm_softc *sc = &apm_softc;
795
796 if (sc->initialized)
797 sc->always_halt_cpu = 1;
798 }
799
800 /* don't halt CPU in scheduling loop */
801 static void
802 apm_not_halt_cpu(void)
803 {
804 struct apm_softc *sc = &apm_softc;
805
806 if (sc->initialized)
807 sc->always_halt_cpu = 0;
808 }
809
810 /* device driver definitions */
811
812 /*
813 * Module event
814 */
815
816 static int
817 apm_modevent(struct module *mod, int event, void *junk)
818 {
819
820 switch (event) {
821 case MOD_LOAD:
822 if (!cold)
823 return (EPERM);
824 break;
825 case MOD_UNLOAD:
826 if (!cold && power_pm_get_type() == POWER_PM_TYPE_APM)
827 return (EBUSY);
828 break;
829 default:
830 break;
831 }
832
833 return (0);
834 }
835
836 /*
837 * Create "connection point"
838 */
839 static void
840 apm_identify(driver_t *driver, device_t parent)
841 {
842 device_t child;
843
844 if (!cold) {
845 printf("Don't load this driver from userland!!\n");
846 return;
847 }
848
849 if (resource_disabled("apm", 0))
850 return;
851
852 child = BUS_ADD_CHILD(parent, 0, "apm", 0);
853 if (child == NULL)
854 panic("apm_identify");
855 }
856
857 /*
858 * probe for APM BIOS
859 */
860 static int
861 apm_probe(device_t dev)
862 {
863 #define APM_KERNBASE KERNBASE
864 struct vm86frame vmf;
865 struct apm_softc *sc = &apm_softc;
866 int flags;
867 #ifdef PC98
868 int rid;
869 #endif
870
871 device_set_desc(dev, "APM BIOS");
872 if (device_get_unit(dev) > 0) {
873 printf("apm: Only one APM driver supported.\n");
874 return ENXIO;
875 }
876
877 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
878 power_pm_get_type() != POWER_PM_TYPE_APM) {
879 printf("apm: Other PM system enabled.\n");
880 return ENXIO;
881 }
882
883 if (resource_int_value("apm", 0, "flags", &flags) != 0)
884 flags = 0;
885
886 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
887 bzero(&apm_softc, sizeof(apm_softc));
888 vmf.vmf_ah = APM_BIOS;
889 vmf.vmf_al = APM_INSTCHECK;
890 vmf.vmf_bx = 0;
891 if (vm86_intcall(APM_INT, &vmf))
892 return ENXIO; /* APM not found */
893 if (vmf.vmf_bx != 0x504d) {
894 printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
895 return ENXIO;
896 }
897 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
898 printf("apm: protected mode connections are not supported\n");
899 return ENXIO;
900 }
901
902 apm_version = vmf.vmf_ax;
903 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
904 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
905 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
906
907 vmf.vmf_ah = APM_BIOS;
908 vmf.vmf_al = APM_DISCONNECT;
909 vmf.vmf_bx = 0;
910 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
911
912 #ifdef PC98
913 /* PC98 have bogos APM 32bit BIOS */
914 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) == 0)
915 return ENXIO;
916 rid = 0;
917 bus_set_resource(dev, SYS_RES_IOPORT, rid,
918 APM_NECSMM_PORT, APM_NECSMM_PORTSZ);
919 sc->sc_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
920 APM_NECSMM_PORT, ~0, APM_NECSMM_PORTSZ, RF_ACTIVE);
921 if (sc->sc_res == NULL) {
922 printf("apm: cannot open NEC smm device\n");
923 return ENXIO;
924 }
925 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_res);
926
927 vmf.vmf_ah = APM_BIOS;
928 vmf.vmf_al = APM_PROT32CONNECT;
929 vmf.vmf_bx = 0;
930 if (vm86_intcall(APM_INT, &vmf)) {
931 printf("apm: 32-bit connection error.\n");
932 return (ENXIO);
933 }
934
935 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
936 sc->bios.seg.code32.limit = 0xffff;
937 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
938 sc->bios.seg.code16.limit = 0xffff;
939 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
940 sc->bios.seg.data.limit = 0xffff;
941 sc->bios.entry = vmf.vmf_ebx;
942 sc->connectmode = APM_PROT32CONNECT;
943 #else
944 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
945 vmf.vmf_ah = APM_BIOS;
946 vmf.vmf_al = APM_PROT32CONNECT;
947 vmf.vmf_bx = 0;
948 if (vm86_intcall(APM_INT, &vmf)) {
949 printf("apm: 32-bit connection error.\n");
950 return (ENXIO);
951 }
952 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
953 sc->bios.seg.code32.limit = 0xffff;
954 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
955 sc->bios.seg.code16.limit = 0xffff;
956 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
957 sc->bios.seg.data.limit = 0xffff;
958 sc->bios.entry = vmf.vmf_ebx;
959 sc->connectmode = APM_PROT32CONNECT;
960 } else {
961 /* use 16-bit connection */
962 vmf.vmf_ah = APM_BIOS;
963 vmf.vmf_al = APM_PROT16CONNECT;
964 vmf.vmf_bx = 0;
965 if (vm86_intcall(APM_INT, &vmf)) {
966 printf("apm: 16-bit connection error.\n");
967 return (ENXIO);
968 }
969 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
970 sc->bios.seg.code16.limit = 0xffff;
971 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
972 sc->bios.seg.data.limit = 0xffff;
973 sc->bios.entry = vmf.vmf_bx;
974 sc->connectmode = APM_PROT16CONNECT;
975 }
976 #endif
977 return(0);
978 }
979
980
981 /*
982 * return 0 if the user will notice and handle the event,
983 * return 1 if the kernel driver should do so.
984 */
985 static int
986 apm_record_event(struct apm_softc *sc, u_int event_type)
987 {
988 struct apm_event_info *evp;
989
990 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
991 return 1; /* no user waiting */
992 if (sc->event_count == APM_NEVENTS)
993 return 1; /* overflow */
994 if (sc->event_filter[event_type] == 0)
995 return 1; /* not registered */
996 evp = &sc->event_list[sc->event_ptr];
997 sc->event_count++;
998 sc->event_ptr++;
999 sc->event_ptr %= APM_NEVENTS;
1000 evp->type = event_type;
1001 evp->index = ++apm_evindex;
1002 selwakeuppri(&sc->sc_rsel, PZERO);
1003 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
1004 }
1005
1006 /* Power profile */
1007 static void
1008 apm_power_profile(struct apm_softc *sc)
1009 {
1010 int state;
1011 struct apm_info info;
1012 static int apm_acline = 0;
1013
1014 if (apm_get_info(&info))
1015 return;
1016
1017 if (apm_acline != info.ai_acline) {
1018 apm_acline = info.ai_acline;
1019 state = apm_acline ? POWER_PROFILE_PERFORMANCE : POWER_PROFILE_ECONOMY;
1020 power_profile_set_state(state);
1021 }
1022 }
1023
1024 /* Process APM event */
1025 static void
1026 apm_processevent(void)
1027 {
1028 int apm_event;
1029 struct apm_softc *sc = &apm_softc;
1030
1031 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
1032 APM_DPRINT("Received APM Event: " #symbol "\n");
1033
1034 do {
1035 apm_event = apm_getevent();
1036 switch (apm_event) {
1037 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
1038 if (apm_op_inprog == 0) {
1039 apm_op_inprog++;
1040 if (apm_record_event(sc, apm_event)) {
1041 apm_suspend(PMST_STANDBY);
1042 }
1043 }
1044 break;
1045 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
1046 if (apm_op_inprog == 0) {
1047 apm_op_inprog++;
1048 if (apm_record_event(sc, apm_event)) {
1049 apm_suspend(PMST_STANDBY);
1050 }
1051 }
1052 break;
1053 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
1054 apm_lastreq_notify();
1055 if (apm_op_inprog == 0) {
1056 apm_op_inprog++;
1057 if (apm_record_event(sc, apm_event)) {
1058 apm_do_suspend();
1059 }
1060 }
1061 return; /* XXX skip the rest */
1062 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
1063 apm_lastreq_notify();
1064 if (apm_op_inprog == 0) {
1065 apm_op_inprog++;
1066 if (apm_record_event(sc, apm_event)) {
1067 apm_do_suspend();
1068 }
1069 }
1070 return; /* XXX skip the rest */
1071 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
1072 apm_do_suspend();
1073 break;
1074 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
1075 apm_record_event(sc, apm_event);
1076 apm_resume();
1077 break;
1078 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
1079 apm_record_event(sc, apm_event);
1080 apm_resume();
1081 break;
1082 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
1083 apm_record_event(sc, apm_event);
1084 break;
1085 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
1086 if (apm_record_event(sc, apm_event)) {
1087 apm_battery_low();
1088 apm_suspend(PMST_SUSPEND);
1089 }
1090 break;
1091 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
1092 apm_record_event(sc, apm_event);
1093 apm_power_profile(sc);
1094 break;
1095 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
1096 apm_record_event(sc, apm_event);
1097 inittodr(0); /* adjust time to RTC */
1098 break;
1099 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
1100 apm_record_event(sc, apm_event);
1101 apm_power_profile(sc);
1102 break;
1103 case PMEV_NOEVENT:
1104 break;
1105 default:
1106 printf("Unknown Original APM Event 0x%x\n", apm_event);
1107 break;
1108 }
1109 } while (apm_event != PMEV_NOEVENT);
1110 #ifdef PC98
1111 apm_disable_smm(sc);
1112 #endif
1113 }
1114
1115 /*
1116 * Attach APM:
1117 *
1118 * Initialize APM driver
1119 */
1120
1121 static int
1122 apm_attach(device_t dev)
1123 {
1124 struct apm_softc *sc = &apm_softc;
1125 int flags;
1126 int drv_version;
1127 #ifdef PC98
1128 int rid;
1129 #endif
1130
1131 if (resource_int_value("apm", 0, "flags", &flags) != 0)
1132 flags = 0;
1133
1134 if (flags & 0x20)
1135 statclock_disable = 1;
1136
1137 sc->initialized = 0;
1138
1139 /* Must be externally enabled */
1140 sc->active = 0;
1141
1142 /* Always call HLT in idle loop */
1143 sc->always_halt_cpu = 1;
1144
1145 getenv_int("debug.apm_debug", &apm_debug);
1146
1147 /* print bootstrap messages */
1148 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1149 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1150 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1151 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1152 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1153 is_enabled(!sc->disabled));
1154 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1155 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1156
1157 #ifdef PC98
1158 rid = 0;
1159 sc->sc_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
1160 APM_NECSMM_PORT, ~0, APM_NECSMM_PORTSZ, RF_ACTIVE);
1161 if (sc->sc_res == NULL)
1162 panic("%s: counldn't map I/O ports", device_get_name(dev));
1163 sc->sc_iot = rman_get_bustag(sc->sc_res);
1164 sc->sc_ioh = rman_get_bushandle(sc->sc_res);
1165
1166 if (apm_version==0x112 || apm_version==0x111 || apm_version==0x110)
1167 apm_necsmm_addr = APM_NECSMM_PORT;
1168 else
1169 apm_necsmm_addr = 0;
1170 apm_necsmm_mask = ~APM_NECSMM_EN;
1171 #endif /* PC98 */
1172
1173 /*
1174 * In one test, apm bios version was 1.02; an attempt to register
1175 * a 1.04 driver resulted in a 1.00 connection! Registering a
1176 * 1.02 driver resulted in a 1.02 connection.
1177 */
1178 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1179 for (; drv_version > 0x100; drv_version--)
1180 if (apm_driver_version(drv_version) == 0)
1181 break;
1182 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1183 ((drv_version & 0x000f) >> 0);
1184 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1185 ((apm_version & 0x0f00) >> 8);
1186
1187 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1188
1189 if (sc->intversion >= INTVERSION(1, 1))
1190 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1191 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1192 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1193 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1194 sc->majorversion, sc->minorversion);
1195
1196
1197 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1198 /* enable power management */
1199 if (sc->disabled) {
1200 if (apm_enable_disable_pm(1)) {
1201 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1202 (sc->bios.r.eax >> 8) & 0xff);
1203 }
1204 }
1205
1206 /* engage power managment (APM 1.1 or later) */
1207 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1208 if (apm_engage_disengage_pm(1)) {
1209 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1210 (sc->bios.r.eax >> 8) & 0xff);
1211 APM_DPRINT(" (Docked or using external power?).\n");
1212 }
1213 }
1214
1215 /* Power the system off using APM */
1216 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1217 SHUTDOWN_PRI_LAST);
1218
1219 /* Register APM again to pass the correct argument of pm_func. */
1220 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, sc);
1221
1222 sc->initialized = 1;
1223 sc->suspending = 0;
1224
1225 make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm");
1226 make_dev(&apm_cdevsw, 8, 0, 5, 0660, "apmctl");
1227 return 0;
1228 }
1229
1230 static int
1231 apmopen(dev_t dev, int flag, int fmt, struct thread *td)
1232 {
1233 struct apm_softc *sc = &apm_softc;
1234 int ctl = APMDEV(dev);
1235
1236 if (!sc->initialized)
1237 return (ENXIO);
1238
1239 switch (ctl) {
1240 case APMDEV_CTL:
1241 if (!(flag & FWRITE))
1242 return EINVAL;
1243 if (sc->sc_flags & SCFLAG_OCTL)
1244 return EBUSY;
1245 sc->sc_flags |= SCFLAG_OCTL;
1246 bzero(sc->event_filter, sizeof sc->event_filter);
1247 break;
1248 case APMDEV_NORMAL:
1249 sc->sc_flags |= SCFLAG_ONORMAL;
1250 break;
1251 default:
1252 return ENXIO;
1253 break;
1254 }
1255 return 0;
1256 }
1257
1258 static int
1259 apmclose(dev_t dev, int flag, int fmt, struct thread *td)
1260 {
1261 struct apm_softc *sc = &apm_softc;
1262 int ctl = APMDEV(dev);
1263
1264 switch (ctl) {
1265 case APMDEV_CTL:
1266 apm_lastreq_rejected();
1267 sc->sc_flags &= ~SCFLAG_OCTL;
1268 bzero(sc->event_filter, sizeof sc->event_filter);
1269 break;
1270 case APMDEV_NORMAL:
1271 sc->sc_flags &= ~SCFLAG_ONORMAL;
1272 break;
1273 }
1274 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1275 sc->event_count = 0;
1276 sc->event_ptr = 0;
1277 }
1278 return 0;
1279 }
1280
1281 static int
1282 apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1283 {
1284 struct apm_softc *sc = &apm_softc;
1285 struct apm_bios_arg *args;
1286 int error = 0;
1287 int ret;
1288 int newstate;
1289
1290 if (!sc->initialized)
1291 return (ENXIO);
1292 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd);
1293 switch (cmd) {
1294 case APMIO_SUSPEND:
1295 if (!(flag & FWRITE))
1296 return (EPERM);
1297 if (sc->active)
1298 apm_suspend(PMST_SUSPEND);
1299 else
1300 error = EINVAL;
1301 break;
1302
1303 case APMIO_STANDBY:
1304 if (!(flag & FWRITE))
1305 return (EPERM);
1306 if (sc->active)
1307 apm_suspend(PMST_STANDBY);
1308 else
1309 error = EINVAL;
1310 break;
1311
1312 case APMIO_GETINFO_OLD:
1313 {
1314 struct apm_info info;
1315 apm_info_old_t aiop;
1316
1317 if (apm_get_info(&info))
1318 error = ENXIO;
1319 aiop = (apm_info_old_t)addr;
1320 aiop->ai_major = info.ai_major;
1321 aiop->ai_minor = info.ai_minor;
1322 aiop->ai_acline = info.ai_acline;
1323 aiop->ai_batt_stat = info.ai_batt_stat;
1324 aiop->ai_batt_life = info.ai_batt_life;
1325 aiop->ai_status = info.ai_status;
1326 }
1327 break;
1328 case APMIO_GETINFO:
1329 if (apm_get_info((apm_info_t)addr))
1330 error = ENXIO;
1331 break;
1332 case APMIO_GETPWSTATUS:
1333 if (apm_get_pwstatus((apm_pwstatus_t)addr))
1334 error = ENXIO;
1335 break;
1336 case APMIO_ENABLE:
1337 if (!(flag & FWRITE))
1338 return (EPERM);
1339 apm_event_enable();
1340 break;
1341 case APMIO_DISABLE:
1342 if (!(flag & FWRITE))
1343 return (EPERM);
1344 apm_event_disable();
1345 break;
1346 case APMIO_HALTCPU:
1347 if (!(flag & FWRITE))
1348 return (EPERM);
1349 apm_halt_cpu();
1350 break;
1351 case APMIO_NOTHALTCPU:
1352 if (!(flag & FWRITE))
1353 return (EPERM);
1354 apm_not_halt_cpu();
1355 break;
1356 case APMIO_DISPLAY:
1357 if (!(flag & FWRITE))
1358 return (EPERM);
1359 newstate = *(int *)addr;
1360 if (apm_display(newstate))
1361 error = ENXIO;
1362 break;
1363 case APMIO_BIOS:
1364 if (!(flag & FWRITE))
1365 return (EPERM);
1366 /* XXX compatibility with the old interface */
1367 args = (struct apm_bios_arg *)addr;
1368 sc->bios.r.eax = args->eax;
1369 sc->bios.r.ebx = args->ebx;
1370 sc->bios.r.ecx = args->ecx;
1371 sc->bios.r.edx = args->edx;
1372 sc->bios.r.esi = args->esi;
1373 sc->bios.r.edi = args->edi;
1374 if ((ret = apm_bioscall())) {
1375 /*
1376 * Return code 1 means bios call was unsuccessful.
1377 * Error code is stored in %ah.
1378 * Return code -1 means bios call was unsupported
1379 * in the APM BIOS version.
1380 */
1381 if (ret == -1) {
1382 error = EINVAL;
1383 }
1384 } else {
1385 /*
1386 * Return code 0 means bios call was successful.
1387 * We need only %al and can discard %ah.
1388 */
1389 sc->bios.r.eax &= 0xff;
1390 }
1391 args->eax = sc->bios.r.eax;
1392 args->ebx = sc->bios.r.ebx;
1393 args->ecx = sc->bios.r.ecx;
1394 args->edx = sc->bios.r.edx;
1395 args->esi = sc->bios.r.esi;
1396 args->edi = sc->bios.r.edi;
1397 break;
1398 default:
1399 error = EINVAL;
1400 break;
1401 }
1402
1403 /* for /dev/apmctl */
1404 if (APMDEV(dev) == APMDEV_CTL) {
1405 struct apm_event_info *evp;
1406 int i;
1407
1408 error = 0;
1409 switch (cmd) {
1410 case APMIO_NEXTEVENT:
1411 if (!sc->event_count) {
1412 error = EAGAIN;
1413 } else {
1414 evp = (struct apm_event_info *)addr;
1415 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1416 i %= APM_NEVENTS;
1417 *evp = sc->event_list[i];
1418 sc->event_count--;
1419 }
1420 break;
1421 case APMIO_REJECTLASTREQ:
1422 if (apm_lastreq_rejected()) {
1423 error = EINVAL;
1424 }
1425 break;
1426 default:
1427 error = EINVAL;
1428 break;
1429 }
1430 }
1431
1432 return error;
1433 }
1434
1435 static int
1436 apmwrite(dev_t dev, struct uio *uio, int ioflag)
1437 {
1438 struct apm_softc *sc = &apm_softc;
1439 u_int event_type;
1440 int error;
1441 u_char enabled;
1442
1443 if (APMDEV(dev) != APMDEV_CTL)
1444 return(ENODEV);
1445 if (uio->uio_resid != sizeof(u_int))
1446 return(E2BIG);
1447
1448 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1449 return(error);
1450
1451 if (event_type < 0 || event_type >= APM_NPMEV)
1452 return(EINVAL);
1453
1454 if (sc->event_filter[event_type] == 0) {
1455 enabled = 1;
1456 } else {
1457 enabled = 0;
1458 }
1459 sc->event_filter[event_type] = enabled;
1460 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1461
1462 return uio->uio_resid;
1463 }
1464
1465 static int
1466 apmpoll(dev_t dev, int events, struct thread *td)
1467 {
1468 struct apm_softc *sc = &apm_softc;
1469 int revents = 0;
1470
1471 if (events & (POLLIN | POLLRDNORM)) {
1472 if (sc->event_count) {
1473 revents |= events & (POLLIN | POLLRDNORM);
1474 } else {
1475 selrecord(td, &sc->sc_rsel);
1476 }
1477 }
1478
1479 return (revents);
1480 }
1481
1482 static device_method_t apm_methods[] = {
1483 /* Device interface */
1484 DEVMETHOD(device_identify, apm_identify),
1485 DEVMETHOD(device_probe, apm_probe),
1486 DEVMETHOD(device_attach, apm_attach),
1487
1488 { 0, 0 }
1489 };
1490
1491 static driver_t apm_driver = {
1492 "apm",
1493 apm_methods,
1494 1, /* no softc (XXX) */
1495 };
1496
1497 static devclass_t apm_devclass;
1498
1499 DRIVER_MODULE(apm, legacy, apm_driver, apm_devclass, apm_modevent, 0);
1500 MODULE_VERSION(apm, 1);
1501
1502 static int
1503 apm_pm_func(u_long cmd, void *arg, ...)
1504 {
1505 int state, apm_state;
1506 int error;
1507 va_list ap;
1508
1509 error = 0;
1510 switch (cmd) {
1511 case POWER_CMD_SUSPEND:
1512 va_start(ap, arg);
1513 state = va_arg(ap, int);
1514 va_end(ap);
1515
1516 switch (state) {
1517 case POWER_SLEEP_STATE_STANDBY:
1518 apm_state = PMST_STANDBY;
1519 break;
1520 case POWER_SLEEP_STATE_SUSPEND:
1521 case POWER_SLEEP_STATE_HIBERNATE:
1522 apm_state = PMST_SUSPEND;
1523 break;
1524 default:
1525 error = EINVAL;
1526 goto out;
1527 }
1528
1529 apm_suspend(apm_state);
1530 break;
1531
1532 default:
1533 error = EINVAL;
1534 goto out;
1535 }
1536
1537 out:
1538 return (error);
1539 }
1540
1541 static void
1542 apm_pm_register(void *arg)
1543 {
1544
1545 if (!resource_disabled("apm", 0))
1546 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, NULL);
1547 }
1548
1549 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, apm_pm_register, 0);
Cache object: 255c3944ae6e58996476ceef36776bf1
|