xref: /freebsd/sys/dev/dpms/dpms.c (revision 2f28bf307c4071d4f4523a59833a5422f4217355)
1 /*-
2  * Copyright (c) 2008 Yahoo!, Inc.
3  * All rights reserved.
4  * Written by: John Baldwin <jhb@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the author nor the names of any co-contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*
32  * Copyright (c) 2004 Benjamin Close <Benjamin.Close@clearchain.com>
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  *
44  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54  * SUCH DAMAGE.
55  */
56 
57 /*
58  * Support for managing the display via DPMS for suspend/resume.
59  */
60 
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
63 
64 #include <sys/param.h>
65 #include <sys/bus.h>
66 #include <sys/kernel.h>
67 #include <sys/libkern.h>
68 #include <sys/module.h>
69 
70 #include <compat/x86bios/x86bios.h>
71 
72 /*
73  * VESA DPMS States
74  */
75 #define DPMS_ON		0x00
76 #define DPMS_STANDBY	0x01
77 #define DPMS_SUSPEND	0x02
78 #define DPMS_OFF	0x04
79 #define DPMS_REDUCEDON	0x08
80 
81 #define	VBE_DPMS_FUNCTION	0x4F10
82 #define	VBE_DPMS_GET_SUPPORTED_STATES 0x00
83 #define	VBE_DPMS_GET_STATE	0x02
84 #define	VBE_DPMS_SET_STATE	0x01
85 #define VBE_MAJORVERSION_MASK	0x0F
86 #define VBE_MINORVERSION_MASK	0xF0
87 
88 struct dpms_softc {
89 	int	dpms_supported_states;
90 	int	dpms_initial_state;
91 };
92 
93 static int	dpms_attach(device_t);
94 static int	dpms_detach(device_t);
95 static int	dpms_get_supported_states(int *);
96 static int	dpms_get_current_state(int *);
97 static void	dpms_identify(driver_t *, device_t);
98 static int	dpms_probe(device_t);
99 static int	dpms_resume(device_t);
100 static int	dpms_set_state(int);
101 static int	dpms_suspend(device_t);
102 
103 static device_method_t dpms_methods[] = {
104 	DEVMETHOD(device_identify,	dpms_identify),
105 	DEVMETHOD(device_probe,		dpms_probe),
106 	DEVMETHOD(device_attach,	dpms_attach),
107 	DEVMETHOD(device_detach,	dpms_detach),
108 	DEVMETHOD(device_suspend,	dpms_suspend),
109 	DEVMETHOD(device_resume,	dpms_resume),
110 	{ 0, 0 }
111 };
112 
113 static driver_t dpms_driver = {
114 	"dpms",
115 	dpms_methods,
116 	sizeof(struct dpms_softc),
117 };
118 
119 static devclass_t dpms_devclass;
120 
121 DRIVER_MODULE(dpms, vgapci, dpms_driver, dpms_devclass, NULL, NULL);
122 MODULE_DEPEND(dpms, x86bios, 1, 1, 1);
123 
124 static void
125 dpms_identify(driver_t *driver, device_t parent)
126 {
127 
128 	if (device_get_flags(parent) != 0 &&
129 	    x86bios_match_device(0xc0000, parent))
130 		device_add_child(parent, "dpms", 0);
131 }
132 
133 static int
134 dpms_probe(device_t dev)
135 {
136 	int error, states;
137 
138 	error = dpms_get_supported_states(&states);
139 	if (error)
140 		return (error);
141 	device_set_desc(dev, "DPMS suspend/resume");
142 	device_quiet(dev);
143 	return (BUS_PROBE_DEFAULT);
144 }
145 
146 static int
147 dpms_attach(device_t dev)
148 {
149 	struct dpms_softc *sc;
150 	int error;
151 
152 	sc = device_get_softc(dev);
153 	error = dpms_get_supported_states(&sc->dpms_supported_states);
154 	if (error)
155 		return (error);
156 	error = dpms_get_current_state(&sc->dpms_initial_state);
157 	return (error);
158 }
159 
160 static int
161 dpms_detach(device_t dev)
162 {
163 
164 	return (0);
165 }
166 
167 static int
168 dpms_suspend(device_t dev)
169 {
170 	struct dpms_softc *sc;
171 
172 	sc = device_get_softc(dev);
173 	if ((sc->dpms_supported_states & DPMS_OFF) != 0)
174 		dpms_set_state(DPMS_OFF);
175 	return (0);
176 }
177 
178 static int
179 dpms_resume(device_t dev)
180 {
181 	struct dpms_softc *sc;
182 
183 	sc = device_get_softc(dev);
184 	dpms_set_state(sc->dpms_initial_state);
185 	return (0);
186 }
187 
188 static int
189 dpms_call_bios(int subfunction, int *bh)
190 {
191 	x86regs_t regs;
192 
193 	if (x86bios_get_intr(0x10) == 0)
194 		return (ENXIO);
195 
196 	x86bios_init_regs(&regs);
197 	regs.R_AX = VBE_DPMS_FUNCTION;
198 	regs.R_BL = subfunction;
199 	regs.R_BH = *bh;
200 	x86bios_intr(&regs, 0x10);
201 
202 	if (regs.R_AX != 0x004f)
203 		return (ENXIO);
204 
205 	*bh = regs.R_BH;
206 
207 	return (0);
208 }
209 
210 static int
211 dpms_get_supported_states(int *states)
212 {
213 
214 	*states = 0;
215 	return (dpms_call_bios(VBE_DPMS_GET_SUPPORTED_STATES, states));
216 }
217 
218 static int
219 dpms_get_current_state(int *state)
220 {
221 
222 	*state = 0;
223 	return (dpms_call_bios(VBE_DPMS_GET_STATE, state));
224 }
225 
226 static int
227 dpms_set_state(int state)
228 {
229 
230 	return (dpms_call_bios(VBE_DPMS_SET_STATE, &state));
231 }
232