dma: create spool files mode 660
[dragonfly.git] / sys / dev / acpica5 / acpi_cmbat.c
blobcf2ffce2e9d950a88727d491c8acbb7b86903a3a
1 /*-
2 * Copyright (c) 2005 Nate Lawson
3 * Copyright (c) 2000 Munehiro Matsuda
4 * Copyright (c) 2000 Takanori Watanabe
5 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * $FreeBSD: src/sys/dev/acpica/acpi_cmbat.c,v 1.46 2007/03/22 18:16:40 jkim Exp $
30 * $DragonFly: src/sys/dev/acpica5/acpi_cmbat.c,v 1.12 2008/09/29 06:59:45 hasso Exp $
33 #include "opt_acpi.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/rman.h>
39 #include <sys/malloc.h>
40 #include <sys/thread2.h>
42 #include "acpi.h"
43 #include <dev/acpica5/acpivar.h>
44 #include <dev/acpica5/acpiio.h>
46 MALLOC_DEFINE(M_ACPICMBAT, "acpicmbat", "ACPI control method battery data");
48 /* Number of times to retry initialization before giving up. */
49 #define ACPI_CMBAT_RETRY_MAX 6
51 /* Check the battery once a minute. */
52 #define CMBAT_POLLRATE (60 * hz)
54 /* Hooks for the ACPI CA debugging infrastructure */
55 #define _COMPONENT ACPI_BATTERY
56 ACPI_MODULE_NAME("BATTERY")
58 #define ACPI_BATTERY_BST_CHANGE 0x80
59 #define ACPI_BATTERY_BIF_CHANGE 0x81
61 struct acpi_cmbat_softc {
62 device_t dev;
63 int flags;
65 struct acpi_bif bif;
66 struct acpi_bst bst;
67 struct timespec bst_lastupdated;
70 static int acpi_cmbat_probe(device_t dev);
71 static int acpi_cmbat_attach(device_t dev);
72 static int acpi_cmbat_detach(device_t dev);
73 static int acpi_cmbat_resume(device_t dev);
74 static void acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify,
75 void *context);
76 static int acpi_cmbat_info_expired(struct timespec *lastupdated);
77 static void acpi_cmbat_info_updated(struct timespec *lastupdated);
78 static void acpi_cmbat_get_bst(void *arg);
79 static void acpi_cmbat_get_bif_task(void *arg);
80 static void acpi_cmbat_get_bif(void *arg);
81 static int acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp);
82 static int acpi_cmbat_bif(device_t dev, struct acpi_bif *bifp);
83 static void acpi_cmbat_init_battery(void *arg);
85 static device_method_t acpi_cmbat_methods[] = {
86 /* Device interface */
87 DEVMETHOD(device_probe, acpi_cmbat_probe),
88 DEVMETHOD(device_attach, acpi_cmbat_attach),
89 DEVMETHOD(device_detach, acpi_cmbat_detach),
90 DEVMETHOD(device_resume, acpi_cmbat_resume),
92 /* ACPI battery interface */
93 DEVMETHOD(acpi_batt_get_info, acpi_cmbat_bif),
94 DEVMETHOD(acpi_batt_get_status, acpi_cmbat_bst),
96 {0, 0}
99 static driver_t acpi_cmbat_driver = {
100 "battery",
101 acpi_cmbat_methods,
102 sizeof(struct acpi_cmbat_softc),
105 static devclass_t acpi_cmbat_devclass;
106 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);
107 MODULE_DEPEND(acpi_cmbat, acpi, 1, 1, 1);
109 static int
110 acpi_cmbat_probe(device_t dev)
112 static char *cmbat_ids[] = { "PNP0C0A", NULL };
114 if (acpi_disabled("cmbat") ||
115 ACPI_ID_PROBE(device_get_parent(dev), dev, cmbat_ids) == NULL)
116 return (ENXIO);
118 device_set_desc(dev, "ACPI Control Method Battery");
119 return (0);
122 static int
123 acpi_cmbat_attach(device_t dev)
125 int error;
126 ACPI_HANDLE handle;
127 struct acpi_cmbat_softc *sc;
129 sc = device_get_softc(dev);
130 handle = acpi_get_handle(dev);
131 sc->dev = dev;
133 timespecclear(&sc->bst_lastupdated);
135 error = acpi_battery_register(dev);
136 if (error != 0) {
137 device_printf(dev, "registering battery failed\n");
138 return (error);
142 * Install a system notify handler in addition to the device notify.
143 * Toshiba notebook uses this alternate notify for its battery.
145 AcpiInstallNotifyHandler(handle, ACPI_ALL_NOTIFY,
146 acpi_cmbat_notify_handler, dev);
148 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
150 return (0);
153 static int
154 acpi_cmbat_detach(device_t dev)
156 ACPI_HANDLE handle;
158 handle = acpi_get_handle(dev);
159 AcpiRemoveNotifyHandler(handle, ACPI_ALL_NOTIFY, acpi_cmbat_notify_handler);
160 acpi_battery_remove(dev);
161 return (0);
164 static int
165 acpi_cmbat_resume(device_t dev)
168 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
169 return (0);
172 static void
173 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
175 struct acpi_cmbat_softc *sc;
176 device_t dev;
178 dev = (device_t)context;
179 sc = device_get_softc(dev);
181 switch (notify) {
182 case ACPI_NOTIFY_DEVICE_CHECK:
183 case ACPI_BATTERY_BST_CHANGE:
185 * Clear the last updated time. The next call to retrieve the
186 * battery status will get the new value for us.
188 timespecclear(&sc->bst_lastupdated);
189 break;
190 case ACPI_NOTIFY_BUS_CHECK:
191 case ACPI_BATTERY_BIF_CHANGE:
193 * Queue a callback to get the current battery info from thread
194 * context. It's not safe to block in a notify handler.
196 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_get_bif_task, dev);
197 break;
200 acpi_UserNotify("CMBAT", h, notify);
203 static int
204 acpi_cmbat_info_expired(struct timespec *lastupdated)
206 struct timespec curtime;
208 if (lastupdated == NULL)
209 return (TRUE);
210 if (!timespecisset(lastupdated))
211 return (TRUE);
213 getnanotime(&curtime);
214 timespecsub(&curtime, lastupdated);
215 return (curtime.tv_sec < 0 ||
216 curtime.tv_sec > acpi_battery_get_info_expire());
219 static void
220 acpi_cmbat_info_updated(struct timespec *lastupdated)
222 if (lastupdated != NULL)
223 getnanotime(lastupdated);
226 static void
227 acpi_cmbat_get_bst(void *arg)
229 struct acpi_cmbat_softc *sc;
230 ACPI_STATUS as;
231 ACPI_OBJECT *res;
232 ACPI_HANDLE h;
233 ACPI_BUFFER bst_buffer;
234 device_t dev;
236 dev = arg;
237 sc = device_get_softc(dev);
238 h = acpi_get_handle(dev);
239 bst_buffer.Pointer = NULL;
240 bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
242 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated))
243 goto end;
245 as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer);
246 if (ACPI_FAILURE(as)) {
247 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
248 "error fetching current battery status -- %s\n",
249 AcpiFormatException(as));
250 goto end;
253 res = (ACPI_OBJECT *)bst_buffer.Pointer;
254 if (!ACPI_PKG_VALID(res, 4)) {
255 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
256 "battery status corrupted\n");
257 goto end;
260 if (acpi_PkgInt32(res, 0, &sc->bst.state) != 0)
261 goto end;
262 if (acpi_PkgInt32(res, 1, &sc->bst.rate) != 0)
263 goto end;
264 if (acpi_PkgInt32(res, 2, &sc->bst.cap) != 0)
265 goto end;
266 if (acpi_PkgInt32(res, 3, &sc->bst.volt) != 0)
267 goto end;
268 acpi_cmbat_info_updated(&sc->bst_lastupdated);
270 /* XXX If all batteries are critical, perhaps we should suspend. */
271 if (sc->bst.state & ACPI_BATT_STAT_CRITICAL) {
272 if ((sc->flags & ACPI_BATT_STAT_CRITICAL) == 0) {
273 sc->flags |= ACPI_BATT_STAT_CRITICAL;
274 device_printf(dev, "critically low charge!\n");
276 } else
277 sc->flags &= ~ACPI_BATT_STAT_CRITICAL;
279 end:
280 if (bst_buffer.Pointer != NULL)
281 AcpiOsFree(bst_buffer.Pointer);
284 /* XXX There should be a cleaner way to do this locking. */
285 static void
286 acpi_cmbat_get_bif_task(void *arg)
288 crit_enter();
289 acpi_cmbat_get_bif(arg);
290 crit_exit();
293 static void
294 acpi_cmbat_get_bif(void *arg)
296 struct acpi_cmbat_softc *sc;
297 ACPI_STATUS as;
298 ACPI_OBJECT *res;
299 ACPI_HANDLE h;
300 ACPI_BUFFER bif_buffer;
301 device_t dev;
303 dev = arg;
304 sc = device_get_softc(dev);
305 h = acpi_get_handle(dev);
306 bif_buffer.Pointer = NULL;
307 bif_buffer.Length = ACPI_ALLOCATE_BUFFER;
309 as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer);
310 if (ACPI_FAILURE(as)) {
311 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
312 "error fetching current battery info -- %s\n",
313 AcpiFormatException(as));
314 goto end;
317 res = (ACPI_OBJECT *)bif_buffer.Pointer;
318 if (!ACPI_PKG_VALID(res, 13)) {
319 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
320 "battery info corrupted\n");
321 goto end;
324 if (acpi_PkgInt32(res, 0, &sc->bif.units) != 0)
325 goto end;
326 if (acpi_PkgInt32(res, 1, &sc->bif.dcap) != 0)
327 goto end;
328 if (acpi_PkgInt32(res, 2, &sc->bif.lfcap) != 0)
329 goto end;
330 if (acpi_PkgInt32(res, 3, &sc->bif.btech) != 0)
331 goto end;
332 if (acpi_PkgInt32(res, 4, &sc->bif.dvol) != 0)
333 goto end;
334 if (acpi_PkgInt32(res, 5, &sc->bif.wcap) != 0)
335 goto end;
336 if (acpi_PkgInt32(res, 6, &sc->bif.lcap) != 0)
337 goto end;
338 if (acpi_PkgInt32(res, 7, &sc->bif.gra1) != 0)
339 goto end;
340 if (acpi_PkgInt32(res, 8, &sc->bif.gra2) != 0)
341 goto end;
342 if (acpi_PkgStr(res, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN) != 0)
343 goto end;
344 if (acpi_PkgStr(res, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN) != 0)
345 goto end;
346 if (acpi_PkgStr(res, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN) != 0)
347 goto end;
348 if (acpi_PkgStr(res, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN) != 0)
349 goto end;
351 end:
352 if (bif_buffer.Pointer != NULL)
353 AcpiOsFree(bif_buffer.Pointer);
356 static int
357 acpi_cmbat_bif(device_t dev, struct acpi_bif *bifp)
359 struct acpi_cmbat_softc *sc;
361 sc = device_get_softc(dev);
364 * Just copy the data. The only value that should change is the
365 * last-full capacity, so we only update when we get a notify that says
366 * the info has changed. Many systems apparently take a long time to
367 * process a _BIF call so we avoid it if possible.
369 crit_enter();
370 bifp->units = sc->bif.units;
371 bifp->dcap = sc->bif.dcap;
372 bifp->lfcap = sc->bif.lfcap;
373 bifp->btech = sc->bif.btech;
374 bifp->dvol = sc->bif.dvol;
375 bifp->wcap = sc->bif.wcap;
376 bifp->lcap = sc->bif.lcap;
377 bifp->gra1 = sc->bif.gra1;
378 bifp->gra2 = sc->bif.gra2;
379 strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
380 strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
381 strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
382 strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
383 crit_exit();
385 return (0);
388 static int
389 acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp)
391 struct acpi_cmbat_softc *sc;
393 sc = device_get_softc(dev);
395 crit_enter();
396 if (acpi_BatteryIsPresent(dev)) {
397 acpi_cmbat_get_bst(dev);
398 bstp->state = sc->bst.state;
399 bstp->rate = sc->bst.rate;
400 bstp->cap = sc->bst.cap;
401 bstp->volt = sc->bst.volt;
402 } else
403 bstp->state = ACPI_BATT_STAT_NOT_PRESENT;
404 crit_exit();
406 return (0);
409 static void
410 acpi_cmbat_init_battery(void *arg)
412 struct acpi_cmbat_softc *sc;
413 int retry, valid;
414 device_t dev;
416 dev = (device_t)arg;
417 sc = device_get_softc(dev);
418 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
419 "battery initialization start\n");
422 * Try repeatedly to get valid data from the battery. Since the
423 * embedded controller isn't always ready just after boot, we may have
424 * to wait a while.
426 for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10)) {
427 /* batteries on DOCK can be ejected w/ DOCK during retrying */
428 if (!device_is_attached(dev))
429 return;
431 if (!acpi_BatteryIsPresent(dev))
432 continue;
435 * Only query the battery if this is the first try or the specific
436 * type of info is still invalid.
438 crit_enter();
439 if (retry == 0 || !acpi_battery_bst_valid(&sc->bst)) {
440 timespecclear(&sc->bst_lastupdated);
441 acpi_cmbat_get_bst(dev);
443 if (retry == 0 || !acpi_battery_bif_valid(&sc->bif))
444 acpi_cmbat_get_bif(dev);
446 valid = acpi_battery_bst_valid(&sc->bst) &&
447 acpi_battery_bif_valid(&sc->bif);
448 crit_exit();
450 if (valid)
451 break;
454 if (retry == ACPI_CMBAT_RETRY_MAX) {
455 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
456 "battery initialization failed, giving up\n");
457 } else {
458 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
459 "battery initialization done, tried %d times\n", retry + 1);