]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/isa/pmtimer.c
Catch up to the new swi API.
[FreeBSD/FreeBSD.git] / sys / i386 / isa / pmtimer.c
1 /*-
2  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD$
27  */
28
29 /*
30  * Timer device driver for power management events.
31  * The code for suspend/resume is derived from APM device driver.
32  */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/syslog.h>
39 #include <machine/clock.h>
40
41 #include <isa/isavar.h>
42
43 /* reject any PnP devices for now */
44 static struct isa_pnp_id pmtimer_ids[] = {
45         {0}
46 };
47
48 static int
49 pmtimer_probe(device_t dev)
50 {
51
52         if (ISA_PNP_PROBE(device_get_parent(dev), dev, pmtimer_ids) == ENXIO) {
53                 return (ENXIO);
54         }
55
56         /* only one instance always */
57         return (device_get_unit(dev));
58 }
59
60 static struct timeval suspend_time;
61 static struct timeval diff_time;
62
63 static int
64 pmtimer_suspend(device_t dev)
65 {
66         int     pl;
67
68         pl = splsoftclock();
69         microtime(&diff_time);
70         inittodr(0);
71         microtime(&suspend_time);
72         timevalsub(&diff_time, &suspend_time);
73         splx(pl);
74         return (0);
75 }
76
77 static int
78 pmtimer_resume(device_t dev)
79 {
80         int pl;
81         u_int second, minute, hour;
82         struct timeval resume_time, tmp_time;
83
84         /* modified for adjkerntz */
85         pl = splsoftclock();
86         i8254_restore();                /* restore timer_freq and hz */
87         inittodr(0);                    /* adjust time to RTC */
88         microtime(&resume_time);
89         getmicrotime(&tmp_time);
90         timevaladd(&tmp_time, &diff_time);
91
92 #ifdef FIXME
93         /* XXX THIS DOESN'T WORK!!! */
94         time = tmp_time;
95 #endif
96
97 #ifdef PMTIMER_FIXUP_CALLTODO
98         /* Calculate the delta time suspended */
99         timevalsub(&resume_time, &suspend_time);
100         /* Fixup the calltodo list with the delta time. */
101         adjust_timeout_calltodo(&resume_time);
102 #endif /* PMTIMER_FIXUP_CALLTODOK */
103         splx(pl);
104 #ifndef PMTIMER_FIXUP_CALLTODO
105         second = resume_time.tv_sec - suspend_time.tv_sec; 
106 #else /* PMTIMER_FIXUP_CALLTODO */
107         /* 
108          * We've already calculated resume_time to be the delta between 
109          * the suspend and the resume. 
110          */
111         second = resume_time.tv_sec; 
112 #endif /* PMTIMER_FIXUP_CALLTODO */
113         hour = second / 3600;
114         second %= 3600;
115         minute = second / 60;
116         second %= 60;
117         log(LOG_NOTICE, "wakeup from sleeping state (slept %02d:%02d:%02d)\n",
118                 hour, minute, second);
119         return (0);
120 }
121
122 static device_method_t pmtimer_methods[] = {
123         /* Device interface */
124         DEVMETHOD(device_probe,         pmtimer_probe),
125         DEVMETHOD(device_attach,        bus_generic_attach),
126         DEVMETHOD(device_suspend,       pmtimer_suspend),
127         DEVMETHOD(device_resume,        pmtimer_resume),
128         { 0, 0 }
129 };
130
131 static driver_t pmtimer_driver = {
132         "pmtimer",
133         pmtimer_methods,
134         1,              /* no softc */
135 };
136
137 static devclass_t pmtimer_devclass;
138
139 DRIVER_MODULE(pmtimer, isa, pmtimer_driver, pmtimer_devclass, 0, 0);