Merge from vendor branch OPENSSH:
[dragonfly.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  * $DragonFly: src/sys/i386/isa/Attic/pmtimer.c,v 1.5 2005/10/28 03:25:57 dillon Exp $
26  */
27
28 #include <sys/cdefs.h>
29
30 /*
31  * Timer device driver for power management events.
32  * The code for suspend/resume is derived from APM device driver.
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/syslog.h>
40 #include <sys/thread2.h>
41 #include <machine/clock.h>
42
43 #include <bus/isa/isavar.h>
44
45 static devclass_t pmtimer_devclass;
46
47 /* reject any PnP devices for now */
48 static struct isa_pnp_id pmtimer_ids[] = {
49         {0}
50 };
51
52 static int
53 pmtimer_probe(device_t dev)
54 {
55
56         if (ISA_PNP_PROBE(device_get_parent(dev), dev, pmtimer_ids) == ENXIO) {
57                 return (ENXIO);
58         }
59
60         /* only one instance always */
61         return (device_get_unit(dev));
62 }
63
64 static struct timeval suspend_time;
65 static struct timeval diff_time;
66
67 static int
68 pmtimer_suspend(device_t dev)
69 {
70         crit_enter();
71         microtime(&diff_time);
72         inittodr(0);
73         microtime(&suspend_time);
74         timevalsub(&diff_time, &suspend_time);
75         crit_exit();
76         return (0);
77 }
78
79 static int
80 pmtimer_resume(device_t dev)
81 {
82         u_int second, minute, hour;
83         struct timeval resume_time;
84
85         /* modified for adjkerntz */
86         crit_enter();
87         timer_restore();                /* restore the all timers */
88         inittodr(0);                    /* adjust time to RTC */
89         microtime(&resume_time);
90
91         crit_exit();
92         second = resume_time.tv_sec - suspend_time.tv_sec; 
93         hour = second / 3600;
94         second %= 3600;
95         minute = second / 60;
96         second %= 60;
97         log(LOG_NOTICE, "wakeup from sleeping state (slept %02d:%02d:%02d)\n",
98                 hour, minute, second);
99         return (0);
100 }
101
102 /*
103  * Because pmtimer is a static device that always exists under any attached
104  * isa device, and not scanned by the isa device, we need an identify
105  * function to install the device.
106  */
107 static device_method_t pmtimer_methods[] = {
108         /* Device interface */
109         DEVMETHOD(device_identify,      bus_generic_identify),
110         DEVMETHOD(device_probe,         pmtimer_probe),
111         DEVMETHOD(device_attach,        bus_generic_attach),
112         DEVMETHOD(device_suspend,       pmtimer_suspend),
113         DEVMETHOD(device_resume,        pmtimer_resume),
114         { 0, 0 }
115 };
116
117 static driver_t pmtimer_driver = {
118         "pmtimer",
119         pmtimer_methods,
120         1,              /* no softc */
121 };
122
123 DRIVER_MODULE(pmtimer, isa, pmtimer_driver, pmtimer_devclass, 0, 0);