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