dpms.c revision 197466
1157299Smarcel/*-
2157299Smarcel * Copyright (c) 2008 Yahoo!, Inc.
3157299Smarcel * All rights reserved.
4157299Smarcel * Written by: John Baldwin <jhb@FreeBSD.org>
5157299Smarcel *
6157299Smarcel * Redistribution and use in source and binary forms, with or without
7157299Smarcel * modification, are permitted provided that the following conditions
8157299Smarcel * are met:
9157299Smarcel * 1. Redistributions of source code must retain the above copyright
10157299Smarcel *    notice, this list of conditions and the following disclaimer.
11157299Smarcel * 2. Redistributions in binary form must reproduce the above copyright
12157299Smarcel *    notice, this list of conditions and the following disclaimer in the
13157299Smarcel *    documentation and/or other materials provided with the distribution.
14157299Smarcel * 3. Neither the name of the author nor the names of any co-contributors
15157299Smarcel *    may be used to endorse or promote products derived from this software
16157299Smarcel *    without specific prior written permission.
17157299Smarcel *
18157299Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19157299Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20157299Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21157299Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22157299Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23157299Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24157299Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25157299Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26157299Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27157299Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28157299Smarcel * SUCH DAMAGE.
29157299Smarcel */
30157299Smarcel
31157299Smarcel/*
32157299Smarcel * Copyright (c) 2004 Benjamin Close <Benjamin.Close@clearchain.com>
33157299Smarcel * All rights reserved.
34157299Smarcel *
35157299Smarcel * Redistribution and use in source and binary forms, with or without
36157299Smarcel * modification, are permitted provided that the following conditions
37157299Smarcel * are met:
38157299Smarcel * 1. Redistributions of source code must retain the above copyright
39157299Smarcel *    notice, this list of conditions and the following disclaimer.
40157299Smarcel * 2. Redistributions in binary form must reproduce the above copyright
41157299Smarcel *    notice, this list of conditions and the following disclaimer in the
42157299Smarcel *    documentation and/or other materials provided with the distribution.
43157299Smarcel *
44157299Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45157351Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46157351Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47157351Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48157299Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49157299Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50157299Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51157299Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52157299Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53157299Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54157299Smarcel * SUCH DAMAGE.
55157299Smarcel */
56157299Smarcel
57157299Smarcel/*
58157299Smarcel * Support for managing the display via DPMS for suspend/resume.
59167822Smarcel */
60157299Smarcel
61157299Smarcel#include <sys/cdefs.h>
62157299Smarcel__FBSDID("$FreeBSD: head/sys/dev/dpms/dpms.c 197466 2009-09-24 19:24:42Z jkim $");
63157299Smarcel
64178600Smarcel#include <sys/param.h>
65178600Smarcel#include <sys/bus.h>
66178600Smarcel#include <sys/kernel.h>
67178600Smarcel#include <sys/libkern.h>
68178600Smarcel#include <sys/module.h>
69178600Smarcel
70178600Smarcel#include <compat/x86bios/x86bios.h>
71157299Smarcel
72157299Smarcel/*
73157299Smarcel * VESA DPMS States
74178600Smarcel */
75157299Smarcel#define DPMS_ON		0x00
76157299Smarcel#define DPMS_STANDBY	0x01
77157299Smarcel#define DPMS_SUSPEND	0x02
78157299Smarcel#define DPMS_OFF	0x04
79157299Smarcel#define DPMS_REDUCEDON	0x08
80157299Smarcel
81157299Smarcel#define	VBE_DPMS_FUNCTION	0x4F10
82157299Smarcel#define	VBE_DPMS_GET_SUPPORTED_STATES 0x00
83227843Smarius#define	VBE_DPMS_GET_STATE	0x02
84227843Smarius#define	VBE_DPMS_SET_STATE	0x01
85157299Smarcel#define VBE_MAJORVERSION_MASK	0x0F
86157299Smarcel#define VBE_MINORVERSION_MASK	0xF0
87157299Smarcel
88157299Smarcelstruct dpms_softc {
89157299Smarcel	int	dpms_supported_states;
90157299Smarcel	int	dpms_initial_state;
91157299Smarcel};
92157299Smarcel
93253900Smariusstatic int	dpms_attach(device_t);
94253900Smariusstatic int	dpms_detach(device_t);
95static int	dpms_get_supported_states(int *);
96static int	dpms_get_current_state(int *);
97static void	dpms_identify(driver_t *, device_t);
98static int	dpms_probe(device_t);
99static int	dpms_resume(device_t);
100static int	dpms_set_state(int);
101static int	dpms_suspend(device_t);
102
103static 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
113static driver_t dpms_driver = {
114	"dpms",
115	dpms_methods,
116	sizeof(struct dpms_softc),
117};
118
119static devclass_t dpms_devclass;
120
121DRIVER_MODULE(dpms, vgapci, dpms_driver, dpms_devclass, NULL, NULL);
122MODULE_DEPEND(dpms, x86bios, 1, 1, 1);
123
124static void
125dpms_identify(driver_t *driver, device_t parent)
126{
127
128	/*
129	 * XXX: The DPMS VBE only allows for manipulating a single
130	 * monitor, but we don't know which one.  Just attach to the
131	 * first vgapci(4) device we encounter and hope it is the
132	 * right one.
133	 */
134	if (devclass_get_device(dpms_devclass, 0) == NULL)
135		device_add_child(parent, "dpms", 0);
136}
137
138static int
139dpms_probe(device_t dev)
140{
141	int error, states;
142
143	error = dpms_get_supported_states(&states);
144	if (error)
145		return (error);
146	device_set_desc(dev, "DPMS suspend/resume");
147	device_quiet(dev);
148	return (BUS_PROBE_DEFAULT);
149}
150
151static int
152dpms_attach(device_t dev)
153{
154	struct dpms_softc *sc;
155	int error;
156
157	sc = device_get_softc(dev);
158	error = dpms_get_supported_states(&sc->dpms_supported_states);
159	if (error)
160		return (error);
161	error = dpms_get_current_state(&sc->dpms_initial_state);
162	return (error);
163}
164
165static int
166dpms_detach(device_t dev)
167{
168
169	return (0);
170}
171
172static int
173dpms_suspend(device_t dev)
174{
175
176	dpms_set_state(DPMS_OFF);
177	return (0);
178}
179
180static int
181dpms_resume(device_t dev)
182{
183	struct dpms_softc *sc;
184
185	sc = device_get_softc(dev);
186	dpms_set_state(sc->dpms_initial_state);
187	return (0);
188}
189
190static int
191dpms_call_bios(int subfunction, int *bh)
192{
193	x86regs_t regs;
194
195	regs.R_AX = VBE_DPMS_FUNCTION;
196	regs.R_BL = subfunction;
197	regs.R_BH = *bh;
198	regs.R_ES = 0;
199	regs.R_DI = 0;
200	x86bios_intr(&regs, 0x10);
201
202	if ((regs.R_EAX & 0xffff) != 0x004f)
203		return (ENXIO);
204
205	*bh = regs.R_BH;
206
207	return (0);
208}
209
210static int
211dpms_get_supported_states(int *states)
212{
213
214	*states = 0;
215	return (dpms_call_bios(VBE_DPMS_GET_SUPPORTED_STATES, states));
216}
217
218static int
219dpms_get_current_state(int *state)
220{
221
222	*state = 0;
223	return (dpms_call_bios(VBE_DPMS_GET_STATE, state));
224}
225
226static int
227dpms_set_state(int state)
228{
229
230	return (dpms_call_bios(VBE_DPMS_SET_STATE, &state));
231}
232