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$");
29
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
41 static 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
62 static struct apm_softc apm_softc;
63
64 static d_open_t apmopen;
65 static d_close_t apmclose;
66 static d_write_t apmwrite;
67 static d_ioctl_t apmioctl;
68 static d_poll_t apmpoll;
69
70 #define CDEV_MAJOR 39
71 static 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
81 static int
82 acpi_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
104 static int
105 acpi_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
130 static int
131 acpi_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
163 static int
164 acpi_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
199 static int
200 apmopen(dev_t dev, int flag, int fmt, d_thread_t *td)
201 {
202 return (0);
203 }
204
205 static int
206 apmclose(dev_t dev, int flag, int fmt, d_thread_t *td)
207 {
208 return (0);
209 }
210
211 static int
212 apmioctl(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
302 static int
303 apmwrite(dev_t dev, struct uio *uio, int ioflag)
304 {
305
306 return (uio->uio_resid);
307 }
308
309 static int
310 apmpoll(dev_t dev, int events, d_thread_t *td)
311 {
312 return (0);
313 }
314
315 static void
316 acpi_capm_init(struct acpi_softc *sc)
317 {
318
319 make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm");
320 }
321
322 int
323 acpi_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
Cache object: 6736d79d8f11fd95d1b97f974e689191
|