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