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