]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/acpica/acpi_cmbat.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / acpica / acpi_cmbat.c
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.
7  *
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.
16  *
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.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
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/ioccom.h>
39
40 #include <machine/bus.h>
41 #include <sys/rman.h>
42 #include <sys/malloc.h>
43
44 #include <contrib/dev/acpica/include/acpi.h>
45
46 #include <dev/acpica/acpivar.h>
47 #include <dev/acpica/acpiio.h>
48
49 MALLOC_DEFINE(M_ACPICMBAT, "acpicmbat", "ACPI control method battery data");
50
51 /* Number of times to retry initialization before giving up. */
52 #define ACPI_CMBAT_RETRY_MAX    6
53
54 /* Check the battery once a minute. */
55 #define CMBAT_POLLRATE          (60 * hz)
56
57 /* Hooks for the ACPI CA debugging infrastructure */
58 #define _COMPONENT      ACPI_BATTERY
59 ACPI_MODULE_NAME("BATTERY")
60
61 #define ACPI_BATTERY_BST_CHANGE 0x80
62 #define ACPI_BATTERY_BIF_CHANGE 0x81
63
64 struct acpi_cmbat_softc {
65     device_t        dev;
66     int             flags;
67
68     struct acpi_bif bif;
69     struct acpi_bst bst;
70     struct timespec bst_lastupdated;
71 };
72
73 ACPI_SERIAL_DECL(cmbat, "ACPI cmbat");
74
75 static int              acpi_cmbat_probe(device_t dev);
76 static int              acpi_cmbat_attach(device_t dev);
77 static int              acpi_cmbat_detach(device_t dev);
78 static int              acpi_cmbat_resume(device_t dev);
79 static void             acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify,
80                             void *context);
81 static int              acpi_cmbat_info_expired(struct timespec *lastupdated);
82 static void             acpi_cmbat_info_updated(struct timespec *lastupdated);
83 static void             acpi_cmbat_get_bst(void *arg);
84 static void             acpi_cmbat_get_bif_task(void *arg);
85 static void             acpi_cmbat_get_bif(void *arg);
86 static int              acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp);
87 static int              acpi_cmbat_bif(device_t dev, struct acpi_bif *bifp);
88 static void             acpi_cmbat_init_battery(void *arg);
89
90 static device_method_t acpi_cmbat_methods[] = {
91     /* Device interface */
92     DEVMETHOD(device_probe,     acpi_cmbat_probe),
93     DEVMETHOD(device_attach,    acpi_cmbat_attach),
94     DEVMETHOD(device_detach,    acpi_cmbat_detach),
95     DEVMETHOD(device_resume,    acpi_cmbat_resume),
96
97     /* ACPI battery interface */
98     DEVMETHOD(acpi_batt_get_info, acpi_cmbat_bif),
99     DEVMETHOD(acpi_batt_get_status, acpi_cmbat_bst),
100
101     {0, 0}
102 };
103
104 static driver_t acpi_cmbat_driver = {
105     "battery",
106     acpi_cmbat_methods,
107     sizeof(struct acpi_cmbat_softc),
108 };
109
110 static devclass_t acpi_cmbat_devclass;
111 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);
112 MODULE_DEPEND(acpi_cmbat, acpi, 1, 1, 1);
113
114 static int
115 acpi_cmbat_probe(device_t dev)
116 {
117     static char *cmbat_ids[] = { "PNP0C0A", NULL };
118
119     if (acpi_disabled("cmbat") ||
120         ACPI_ID_PROBE(device_get_parent(dev), dev, cmbat_ids) == NULL)
121         return (ENXIO);
122
123     device_set_desc(dev, "ACPI Control Method Battery");
124     return (0);
125 }
126
127 static int
128 acpi_cmbat_attach(device_t dev)
129 {
130     int         error;
131     ACPI_HANDLE handle;
132     struct acpi_cmbat_softc *sc;
133
134     sc = device_get_softc(dev);
135     handle = acpi_get_handle(dev);
136     sc->dev = dev;
137
138     timespecclear(&sc->bst_lastupdated);
139
140     error = acpi_battery_register(dev);
141     if (error != 0) {
142         device_printf(dev, "registering battery failed\n");
143         return (error);
144     }
145
146     /*
147      * Install a system notify handler in addition to the device notify.
148      * Toshiba notebook uses this alternate notify for its battery.
149      */
150     AcpiInstallNotifyHandler(handle, ACPI_ALL_NOTIFY,
151         acpi_cmbat_notify_handler, dev);
152
153     AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
154
155     return (0);
156 }
157
158 static int
159 acpi_cmbat_detach(device_t dev)
160 {
161     ACPI_HANDLE handle;
162
163     handle = acpi_get_handle(dev);
164     AcpiRemoveNotifyHandler(handle, ACPI_ALL_NOTIFY, acpi_cmbat_notify_handler);
165     acpi_battery_remove(dev);
166     return (0);
167 }
168
169 static int
170 acpi_cmbat_resume(device_t dev)
171 {
172
173     AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
174     return (0);
175 }
176
177 static void
178 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
179 {
180     struct acpi_cmbat_softc *sc;
181     device_t dev;
182
183     dev = (device_t)context;
184     sc = device_get_softc(dev);
185
186     switch (notify) {
187     case ACPI_NOTIFY_DEVICE_CHECK:
188     case ACPI_BATTERY_BST_CHANGE:
189         /*
190          * Clear the last updated time.  The next call to retrieve the
191          * battery status will get the new value for us.
192          */
193         timespecclear(&sc->bst_lastupdated);
194         break;
195     case ACPI_NOTIFY_BUS_CHECK:
196     case ACPI_BATTERY_BIF_CHANGE:
197         /*
198          * Queue a callback to get the current battery info from thread
199          * context.  It's not safe to block in a notify handler.
200          */
201         AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_get_bif_task, dev);
202         break;
203     }
204
205     acpi_UserNotify("CMBAT", h, notify);
206 }
207
208 static int
209 acpi_cmbat_info_expired(struct timespec *lastupdated)
210 {
211     struct timespec     curtime;
212
213     ACPI_SERIAL_ASSERT(cmbat);
214
215     if (lastupdated == NULL)
216         return (TRUE);
217     if (!timespecisset(lastupdated))
218         return (TRUE);
219
220     getnanotime(&curtime);
221     timespecsub(&curtime, lastupdated);
222     return (curtime.tv_sec < 0 ||
223             curtime.tv_sec > acpi_battery_get_info_expire());
224 }
225
226 static void
227 acpi_cmbat_info_updated(struct timespec *lastupdated)
228 {
229
230     ACPI_SERIAL_ASSERT(cmbat);
231
232     if (lastupdated != NULL)
233         getnanotime(lastupdated);
234 }
235
236 static void
237 acpi_cmbat_get_bst(void *arg)
238 {
239     struct acpi_cmbat_softc *sc;
240     ACPI_STATUS as;
241     ACPI_OBJECT *res;
242     ACPI_HANDLE h;
243     ACPI_BUFFER bst_buffer;
244     device_t dev;
245
246     ACPI_SERIAL_ASSERT(cmbat);
247
248     dev = arg;
249     sc = device_get_softc(dev);
250     h = acpi_get_handle(dev);
251     bst_buffer.Pointer = NULL;
252     bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
253
254     if (!acpi_cmbat_info_expired(&sc->bst_lastupdated))
255         goto end;
256
257     as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer);
258     if (ACPI_FAILURE(as)) {
259         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
260                     "error fetching current battery status -- %s\n",
261                     AcpiFormatException(as));
262         goto end;
263     }
264
265     res = (ACPI_OBJECT *)bst_buffer.Pointer;
266     if (!ACPI_PKG_VALID(res, 4)) {
267         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
268                     "battery status corrupted\n");
269         goto end;
270     }
271
272     if (acpi_PkgInt32(res, 0, &sc->bst.state) != 0)
273         goto end;
274     if (acpi_PkgInt32(res, 1, &sc->bst.rate) != 0)
275         goto end;
276     if (acpi_PkgInt32(res, 2, &sc->bst.cap) != 0)
277         goto end;
278     if (acpi_PkgInt32(res, 3, &sc->bst.volt) != 0)
279         goto end;
280     acpi_cmbat_info_updated(&sc->bst_lastupdated);
281
282     /* XXX If all batteries are critical, perhaps we should suspend. */
283     if (sc->bst.state & ACPI_BATT_STAT_CRITICAL) {
284         if ((sc->flags & ACPI_BATT_STAT_CRITICAL) == 0) {
285             sc->flags |= ACPI_BATT_STAT_CRITICAL;
286             device_printf(dev, "critically low charge!\n");
287         }
288     } else
289         sc->flags &= ~ACPI_BATT_STAT_CRITICAL;
290
291 end:
292     if (bst_buffer.Pointer != NULL)
293         AcpiOsFree(bst_buffer.Pointer);
294 }
295
296 /* XXX There should be a cleaner way to do this locking. */
297 static void
298 acpi_cmbat_get_bif_task(void *arg)
299 {
300
301     ACPI_SERIAL_BEGIN(cmbat);
302     acpi_cmbat_get_bif(arg);
303     ACPI_SERIAL_END(cmbat);
304 }
305
306 static void
307 acpi_cmbat_get_bif(void *arg)
308 {
309     struct acpi_cmbat_softc *sc;
310     ACPI_STATUS as;
311     ACPI_OBJECT *res;
312     ACPI_HANDLE h;
313     ACPI_BUFFER bif_buffer;
314     device_t dev;
315
316     ACPI_SERIAL_ASSERT(cmbat);
317
318     dev = arg;
319     sc = device_get_softc(dev);
320     h = acpi_get_handle(dev);
321     bif_buffer.Pointer = NULL;
322     bif_buffer.Length = ACPI_ALLOCATE_BUFFER;
323
324     as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer);
325     if (ACPI_FAILURE(as)) {
326         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
327                     "error fetching current battery info -- %s\n",
328                     AcpiFormatException(as));
329         goto end;
330     }
331
332     res = (ACPI_OBJECT *)bif_buffer.Pointer;
333     if (!ACPI_PKG_VALID(res, 13)) {
334         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
335                     "battery info corrupted\n");
336         goto end;
337     }
338
339     if (acpi_PkgInt32(res, 0, &sc->bif.units) != 0)
340         goto end;
341     if (acpi_PkgInt32(res, 1, &sc->bif.dcap) != 0)
342         goto end;
343     if (acpi_PkgInt32(res, 2, &sc->bif.lfcap) != 0)
344         goto end;
345     if (acpi_PkgInt32(res, 3, &sc->bif.btech) != 0)
346         goto end;
347     if (acpi_PkgInt32(res, 4, &sc->bif.dvol) != 0)
348         goto end;
349     if (acpi_PkgInt32(res, 5, &sc->bif.wcap) != 0)
350         goto end;
351     if (acpi_PkgInt32(res, 6, &sc->bif.lcap) != 0)
352         goto end;
353     if (acpi_PkgInt32(res, 7, &sc->bif.gra1) != 0)
354         goto end;
355     if (acpi_PkgInt32(res, 8, &sc->bif.gra2) != 0)
356         goto end;
357     if (acpi_PkgStr(res,  9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN) != 0)
358         goto end;
359     if (acpi_PkgStr(res, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN) != 0)
360         goto end;
361     if (acpi_PkgStr(res, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN) != 0)
362         goto end;
363     if (acpi_PkgStr(res, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN) != 0)
364         goto end;
365
366 end:
367     if (bif_buffer.Pointer != NULL)
368         AcpiOsFree(bif_buffer.Pointer);
369 }
370
371 static int
372 acpi_cmbat_bif(device_t dev, struct acpi_bif *bifp)
373 {
374     struct acpi_cmbat_softc *sc;
375
376     sc = device_get_softc(dev);
377
378     /*
379      * Just copy the data.  The only value that should change is the
380      * last-full capacity, so we only update when we get a notify that says
381      * the info has changed.  Many systems apparently take a long time to
382      * process a _BIF call so we avoid it if possible.
383      */
384     ACPI_SERIAL_BEGIN(cmbat);
385     bifp->units = sc->bif.units;
386     bifp->dcap = sc->bif.dcap;
387     bifp->lfcap = sc->bif.lfcap;
388     bifp->btech = sc->bif.btech;
389     bifp->dvol = sc->bif.dvol;
390     bifp->wcap = sc->bif.wcap;
391     bifp->lcap = sc->bif.lcap;
392     bifp->gra1 = sc->bif.gra1;
393     bifp->gra2 = sc->bif.gra2;
394     strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
395     strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
396     strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
397     strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
398     ACPI_SERIAL_END(cmbat);
399
400     return (0);
401 }
402
403 static int
404 acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp)
405 {
406     struct acpi_cmbat_softc *sc;
407
408     sc = device_get_softc(dev);
409
410     ACPI_SERIAL_BEGIN(cmbat);
411     if (acpi_BatteryIsPresent(dev)) {
412         acpi_cmbat_get_bst(dev);
413         bstp->state = sc->bst.state;
414         bstp->rate = sc->bst.rate;
415         bstp->cap = sc->bst.cap;
416         bstp->volt = sc->bst.volt;
417     } else
418         bstp->state = ACPI_BATT_STAT_NOT_PRESENT;
419     ACPI_SERIAL_END(cmbat);
420
421     return (0);
422 }
423
424 static void
425 acpi_cmbat_init_battery(void *arg)
426 {
427     struct acpi_cmbat_softc *sc;
428     int         retry, valid;
429     device_t    dev;
430
431     dev = (device_t)arg;
432     sc = device_get_softc(dev);
433     ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
434                 "battery initialization start\n");
435
436     /*
437      * Try repeatedly to get valid data from the battery.  Since the
438      * embedded controller isn't always ready just after boot, we may have
439      * to wait a while.
440      */
441     for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10000)) {
442         /* batteries on DOCK can be ejected w/ DOCK during retrying */
443         if (!device_is_attached(dev))
444             return;
445
446         if (!acpi_BatteryIsPresent(dev))
447             continue;
448
449         /*
450          * Only query the battery if this is the first try or the specific
451          * type of info is still invalid.
452          */
453         ACPI_SERIAL_BEGIN(cmbat);
454         if (retry == 0 || !acpi_battery_bst_valid(&sc->bst)) {
455             timespecclear(&sc->bst_lastupdated);
456             acpi_cmbat_get_bst(dev);
457         }
458         if (retry == 0 || !acpi_battery_bif_valid(&sc->bif))
459             acpi_cmbat_get_bif(dev);
460
461         valid = acpi_battery_bst_valid(&sc->bst) &&
462             acpi_battery_bif_valid(&sc->bif);
463         ACPI_SERIAL_END(cmbat);
464
465         if (valid)
466             break;
467     }
468
469     if (retry == ACPI_CMBAT_RETRY_MAX) {
470         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
471                     "battery initialization failed, giving up\n");
472     } else {
473         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
474                     "battery initialization done, tried %d times\n", retry + 1);
475     }
476 }