acpi_apm.c revision 126076
1/*-
2 * Copyright (c) 2001 Mitsuru IWASAKI
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
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/i386/acpica/acpi_machdep.c 126076 2004-02-21 19:42:58Z phk $");
29
30#include <sys/param.h>
31#include <sys/bus.h>
32#include <sys/conf.h>
33#include <sys/fcntl.h>
34#include <sys/kernel.h>
35#include <sys/sysctl.h>
36#include <sys/uio.h>
37
38#include "acpi.h"
39
40#include <dev/acpica/acpivar.h>
41#include <dev/acpica/acpiio.h>
42
43static device_t	acpi_dev;
44
45/*
46 * APM driver emulation
47 */
48
49#if __FreeBSD_version < 500000
50#include <sys/select.h>
51#else
52#include <sys/selinfo.h>
53#endif
54
55#include <machine/apm_bios.h>
56#include <machine/pc/bios.h>
57
58#if __FreeBSD_version < 500000
59#include <i386/apm/apm.h>
60#else
61#include <i386/bios/apm.h>
62#endif
63
64u_int32_t acpi_reset_video = 1;
65TUNABLE_INT("hw.acpi.reset_video", &acpi_reset_video);
66
67static struct apm_softc	apm_softc;
68
69static d_open_t apmopen;
70static d_close_t apmclose;
71static d_write_t apmwrite;
72static d_ioctl_t apmioctl;
73static d_poll_t apmpoll;
74
75static struct cdevsw apm_cdevsw = {
76	.d_open =	apmopen,
77	.d_close =	apmclose,
78	.d_write =	apmwrite,
79	.d_ioctl =	apmioctl,
80	.d_poll =	apmpoll,
81	.d_name =	"apm",
82};
83
84static int intr_model = ACPI_INTR_PIC;
85
86static int
87acpi_capm_convert_battstate(struct  acpi_battinfo *battp)
88{
89	int	state;
90
91	state = 0xff;	/* XXX unknown */
92
93	if (battp->state & ACPI_BATT_STAT_DISCHARG) {
94		if (battp->cap >= 50)
95			state = 0;	/* high */
96		else
97			state = 1;	/* low */
98	}
99	if (battp->state & ACPI_BATT_STAT_CRITICAL)
100		state = 2;		/* critical */
101	if (battp->state & ACPI_BATT_STAT_CHARGING)
102		state = 3;		/* charging */
103
104	/* If still unknown, determine it based on the battery capacity. */
105	if (state == 0xff) {
106		if (battp->cap >= 50) {
107			state = 0;	/* high */
108		} else {
109			state = 1;	/* low */
110		}
111	}
112
113	return (state);
114}
115
116static int
117acpi_capm_convert_battflags(struct  acpi_battinfo *battp)
118{
119	int	flags;
120
121	flags = 0;
122
123	if (battp->cap >= 50) {
124		flags |= APM_BATT_HIGH;
125	} else {
126		if (battp->state & ACPI_BATT_STAT_CRITICAL)
127			flags |= APM_BATT_CRITICAL;
128		else
129			flags |= APM_BATT_LOW;
130	}
131	if (battp->state & ACPI_BATT_STAT_CHARGING)
132		flags |= APM_BATT_CHARGING;
133	if (battp->state == ACPI_BATT_STAT_NOT_PRESENT)
134		flags = APM_BATT_NOT_PRESENT;
135
136	return (flags);
137}
138
139static int
140acpi_capm_get_info(apm_info_t aip)
141{
142	int	acline;
143	struct	acpi_battinfo batt;
144
145	aip->ai_infoversion = 1;
146	aip->ai_major       = 1;
147	aip->ai_minor       = 2;
148	aip->ai_status      = apm_softc.active;
149	aip->ai_capabilities= 0xff00;	/* XXX unknown */
150
151	if (acpi_acad_get_acline(&acline))
152		aip->ai_acline = 0xff;		/* unknown */
153	else
154		aip->ai_acline = acline;	/* on/off */
155
156	if (acpi_battery_get_battinfo(-1, &batt)) {
157		aip->ai_batt_stat = 0xff;	/* unknown */
158		aip->ai_batt_life = 0xff;	/* unknown */
159		aip->ai_batt_time = -1;		/* unknown */
160		aip->ai_batteries = 0;
161	} else {
162		aip->ai_batt_stat = acpi_capm_convert_battstate(&batt);
163		aip->ai_batt_life = batt.cap;
164		aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
165		aip->ai_batteries = acpi_battery_get_units();
166	}
167
168	return (0);
169}
170
171static int
172acpi_capm_get_pwstatus(apm_pwstatus_t app)
173{
174	int	batt_unit;
175	int	acline;
176	struct	acpi_battinfo batt;
177
178	if (app->ap_device != PMDV_ALLDEV &&
179	    (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL)) {
180		return (1);
181	}
182
183	if (app->ap_device == PMDV_ALLDEV)
184		batt_unit = -1;			/* all units */
185	else
186		batt_unit = app->ap_device - PMDV_BATT0;
187
188	if (acpi_battery_get_battinfo(batt_unit, &batt))
189		return (1);
190
191	app->ap_batt_stat = acpi_capm_convert_battstate(&batt);
192	app->ap_batt_flag = acpi_capm_convert_battflags(&batt);
193	app->ap_batt_life = batt.cap;
194	app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
195
196	if (acpi_acad_get_acline(&acline))
197		app->ap_acline = 0xff;		/* unknown */
198	else
199		app->ap_acline = acline;	/* on/off */
200
201	return (0);
202}
203
204static int
205apmopen(dev_t dev, int flag, int fmt, d_thread_t *td)
206{
207	return (0);
208}
209
210static int
211apmclose(dev_t dev, int flag, int fmt, d_thread_t *td)
212{
213	return (0);
214}
215
216static int
217apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
218{
219	int	error = 0;
220	struct	acpi_softc *acpi_sc;
221	struct apm_info info;
222	apm_info_old_t aiop;
223
224	if ((acpi_sc = device_get_softc(acpi_dev)) == NULL)
225		return (ENXIO);
226
227	switch (cmd) {
228	case APMIO_SUSPEND:
229		if ((flag & FWRITE) == 0)
230			return (EPERM);
231		if (apm_softc.active)
232			acpi_SetSleepState(acpi_sc, acpi_sc->acpi_suspend_sx);
233		else
234			error = EINVAL;
235		break;
236	case APMIO_STANDBY:
237		if ((flag & FWRITE) == 0)
238			return (EPERM);
239		if (apm_softc.active)
240			acpi_SetSleepState(acpi_sc, acpi_sc->acpi_standby_sx);
241		else
242			error = EINVAL;
243		break;
244	case APMIO_GETINFO_OLD:
245		if (acpi_capm_get_info(&info))
246			error = ENXIO;
247		aiop = (apm_info_old_t)addr;
248		aiop->ai_major = info.ai_major;
249		aiop->ai_minor = info.ai_minor;
250		aiop->ai_acline = info.ai_acline;
251		aiop->ai_batt_stat = info.ai_batt_stat;
252		aiop->ai_batt_life = info.ai_batt_life;
253		aiop->ai_status = info.ai_status;
254		break;
255	case APMIO_GETINFO:
256		if (acpi_capm_get_info((apm_info_t)addr))
257			error = ENXIO;
258		break;
259	case APMIO_GETPWSTATUS:
260		if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr))
261			error = ENXIO;
262		break;
263	case APMIO_ENABLE:
264		if ((flag & FWRITE) == 0)
265			return (EPERM);
266		apm_softc.active = 1;
267		break;
268	case APMIO_DISABLE:
269		if ((flag & FWRITE) == 0)
270			return (EPERM);
271		apm_softc.active = 0;
272		break;
273	case APMIO_HALTCPU:
274		break;
275	case APMIO_NOTHALTCPU:
276		break;
277	case APMIO_DISPLAY:
278		if ((flag & FWRITE) == 0)
279			return (EPERM);
280		break;
281	case APMIO_BIOS:
282		if ((flag & FWRITE) == 0)
283			return (EPERM);
284		bzero(addr, sizeof(struct apm_bios_arg));
285		break;
286	default:
287		error = EINVAL;
288		break;
289	}
290
291	return (error);
292}
293
294static int
295apmwrite(dev_t dev, struct uio *uio, int ioflag)
296{
297	return (uio->uio_resid);
298}
299
300static int
301apmpoll(dev_t dev, int events, d_thread_t *td)
302{
303	return (0);
304}
305
306static void
307acpi_capm_init(struct acpi_softc *sc)
308{
309        make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm");
310}
311
312int
313acpi_machdep_init(device_t dev)
314{
315	struct	acpi_softc *sc;
316
317	acpi_dev = dev;
318	if ((sc = device_get_softc(acpi_dev)) == NULL)
319		return (ENXIO);
320
321	/*
322	 * XXX: Prevent the PnP BIOS code from interfering with
323	 * our own scan of ISA devices.
324	 */
325	PnPBIOStable = NULL;
326
327	acpi_capm_init(sc);
328
329	acpi_install_wakeup_handler(sc);
330
331	if (intr_model != ACPI_INTR_PIC)
332		acpi_SetIntrModel(intr_model);
333
334	SYSCTL_ADD_UINT(&sc->acpi_sysctl_ctx,
335	    SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO,
336	    "reset_video", CTLFLAG_RD | CTLFLAG_RW, &acpi_reset_video, 0,
337	    "Call the VESA reset BIOS vector on the resume path");
338
339	return (0);
340}
341
342void
343acpi_SetDefaultIntrModel(int model)
344{
345
346	intr_model = model;
347}
348