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