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
167 /*
168 * Force any pending notification handler calls to complete by
169 * requesting cmbat serialisation while freeing and clearing the
170 * softc pointer:
171 */
172 ACPI_SERIAL_BEGIN(cmbat);
173 device_set_softc(dev, NULL);
174 ACPI_SERIAL_END(cmbat);
175
176 return (0);
177 }
178
179 static int
180 acpi_cmbat_resume(device_t dev)
181 {
182
183 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
184 return (0);
185 }
186
187 static void
188 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
189 {
190 struct acpi_cmbat_softc *sc;
191 device_t dev;
192
193 dev = (device_t)context;
194 sc = device_get_softc(dev);
195
196 switch (notify) {
197 case ACPI_NOTIFY_DEVICE_CHECK:
198 case ACPI_BATTERY_BST_CHANGE:
199 /*
200 * Clear the last updated time. The next call to retrieve the
201 * battery status will get the new value for us.
202 */
203 timespecclear(&sc->bst_lastupdated);
204 break;
205 case ACPI_NOTIFY_BUS_CHECK:
206 case ACPI_BATTERY_BIF_CHANGE:
207 /*
208 * Queue a callback to get the current battery info from thread
209 * context. It's not safe to block in a notify handler.
210 */
211 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_get_bif_task, dev);
212 break;
213 }
214
215 acpi_UserNotify("CMBAT", h, notify);
216 }
217
218 static int
219 acpi_cmbat_info_expired(struct timespec *lastupdated)
220 {
221 struct timespec curtime;
222
223 ACPI_SERIAL_ASSERT(cmbat);
224
225 if (lastupdated == NULL)
226 return (TRUE);
227 if (!timespecisset(lastupdated))
228 return (TRUE);
229
230 getnanotime(&curtime);
231 timespecsub(&curtime, lastupdated);
232 return (curtime.tv_sec < 0 ||
233 curtime.tv_sec > acpi_battery_get_info_expire());
234 }
235
236 static void
237 acpi_cmbat_info_updated(struct timespec *lastupdated)
238 {
239
240 ACPI_SERIAL_ASSERT(cmbat);
241
242 if (lastupdated != NULL)
243 getnanotime(lastupdated);
244 }
245
246 static void
247 acpi_cmbat_get_bst(void *arg)
248 {
249 struct acpi_cmbat_softc *sc;
250 ACPI_STATUS as;
251 ACPI_OBJECT *res;
252 ACPI_HANDLE h;
253 ACPI_BUFFER bst_buffer;
254 device_t dev;
255
256 ACPI_SERIAL_ASSERT(cmbat);
257
258 dev = arg;
259 sc = device_get_softc(dev);
260 h = acpi_get_handle(dev);
261 bst_buffer.Pointer = NULL;
262 bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
263
264 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated))
265 goto end;
266
267 as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer);
268 if (ACPI_FAILURE(as)) {
269 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
270 "error fetching current battery status -- %s\n",
271 AcpiFormatException(as));
272 goto end;
273 }
274
275 res = (ACPI_OBJECT *)bst_buffer.Pointer;
276 if (!ACPI_PKG_VALID(res, 4)) {
277 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
278 "battery status corrupted\n");
279 goto end;
280 }
281
282 if (acpi_PkgInt32(res, 0, &sc->bst.state) != 0)
283 goto end;
284 if (acpi_PkgInt32(res, 1, &sc->bst.rate) != 0)
285 goto end;
286 if (acpi_PkgInt32(res, 2, &sc->bst.cap) != 0)
287 goto end;
288 if (acpi_PkgInt32(res, 3, &sc->bst.volt) != 0)
289 goto end;
290 acpi_cmbat_info_updated(&sc->bst_lastupdated);
291
292 /* Clear out undefined/extended bits that might be set by hardware. */
293 sc->bst.state &= ACPI_BATT_STAT_BST_MASK;
294 if ((sc->bst.state & ACPI_BATT_STAT_INVALID) == ACPI_BATT_STAT_INVALID)
295 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
296 "battery reports simultaneous charging and discharging\n");
297
298 /* XXX If all batteries are critical, perhaps we should suspend. */
299 if (sc->bst.state & ACPI_BATT_STAT_CRITICAL) {
300 if ((sc->flags & ACPI_BATT_STAT_CRITICAL) == 0) {
301 sc->flags |= ACPI_BATT_STAT_CRITICAL;
302 device_printf(dev, "critically low charge!\n");
303 }
304 } else
305 sc->flags &= ~ACPI_BATT_STAT_CRITICAL;
306
307 end:
308 if (bst_buffer.Pointer != NULL)
309 AcpiOsFree(bst_buffer.Pointer);
310 }
311
312 /* XXX There should be a cleaner way to do this locking. */
313 static void
314 acpi_cmbat_get_bif_task(void *arg)
315 {
316
317 ACPI_SERIAL_BEGIN(cmbat);
318 acpi_cmbat_get_bif(arg);
319 ACPI_SERIAL_END(cmbat);
320 }
321
322 static void
323 acpi_cmbat_get_bif(void *arg)
324 {
325 struct acpi_cmbat_softc *sc;
326 ACPI_STATUS as;
327 ACPI_OBJECT *res;
328 ACPI_HANDLE h;
329 ACPI_BUFFER bif_buffer;
330 device_t dev;
331
332 ACPI_SERIAL_ASSERT(cmbat);
333
334 dev = arg;
335 sc = device_get_softc(dev);
336 h = acpi_get_handle(dev);
337 bif_buffer.Pointer = NULL;
338 bif_buffer.Length = ACPI_ALLOCATE_BUFFER;
339
340 as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer);
341 if (ACPI_FAILURE(as)) {
342 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
343 "error fetching current battery info -- %s\n",
344 AcpiFormatException(as));
345 goto end;
346 }
347
348 res = (ACPI_OBJECT *)bif_buffer.Pointer;
349 if (!ACPI_PKG_VALID(res, 13)) {
350 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
351 "battery info corrupted\n");
352 goto end;
353 }
354
355 if (acpi_PkgInt32(res, 0, &sc->bif.units) != 0)
356 goto end;
357 if (acpi_PkgInt32(res, 1, &sc->bif.dcap) != 0)
358 goto end;
359 if (acpi_PkgInt32(res, 2, &sc->bif.lfcap) != 0)
360 goto end;
361 if (acpi_PkgInt32(res, 3, &sc->bif.btech) != 0)
362 goto end;
363 if (acpi_PkgInt32(res, 4, &sc->bif.dvol) != 0)
364 goto end;
365 if (acpi_PkgInt32(res, 5, &sc->bif.wcap) != 0)
366 goto end;
367 if (acpi_PkgInt32(res, 6, &sc->bif.lcap) != 0)
368 goto end;
369 if (acpi_PkgInt32(res, 7, &sc->bif.gra1) != 0)
370 goto end;
371 if (acpi_PkgInt32(res, 8, &sc->bif.gra2) != 0)
372 goto end;
373 if (acpi_PkgStr(res, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN) != 0)
374 goto end;
375 if (acpi_PkgStr(res, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN) != 0)
376 goto end;
377 if (acpi_PkgStr(res, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN) != 0)
378 goto end;
379 if (acpi_PkgStr(res, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN) != 0)
380 goto end;
381
382 end:
383 if (bif_buffer.Pointer != NULL)
384 AcpiOsFree(bif_buffer.Pointer);
385 }
386
387 static int
388 acpi_cmbat_bif(device_t dev, struct acpi_bif *bifp)
389 {
390 struct acpi_cmbat_softc *sc;
391
392 sc = device_get_softc(dev);
393
394 /*
395 * Just copy the data. The only value that should change is the
396 * last-full capacity, so we only update when we get a notify that says
397 * the info has changed. Many systems apparently take a long time to
398 * process a _BIF call so we avoid it if possible.
399 */
400 ACPI_SERIAL_BEGIN(cmbat);
401 bifp->units = sc->bif.units;
402 bifp->dcap = sc->bif.dcap;
403 bifp->lfcap = sc->bif.lfcap;
404 bifp->btech = sc->bif.btech;
405 bifp->dvol = sc->bif.dvol;
406 bifp->wcap = sc->bif.wcap;
407 bifp->lcap = sc->bif.lcap;
408 bifp->gra1 = sc->bif.gra1;
409 bifp->gra2 = sc->bif.gra2;
410 strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
411 strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
412 strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
413 strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
414 ACPI_SERIAL_END(cmbat);
415
416 return (0);
417 }
418
419 static int
420 acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp)
421 {
422 struct acpi_cmbat_softc *sc;
423
424 sc = device_get_softc(dev);
425
426 ACPI_SERIAL_BEGIN(cmbat);
427 if (acpi_BatteryIsPresent(dev)) {
428 acpi_cmbat_get_bst(dev);
429 bstp->state = sc->bst.state;
430 bstp->rate = sc->bst.rate;
431 bstp->cap = sc->bst.cap;
432 bstp->volt = sc->bst.volt;
433 } else
434 bstp->state = ACPI_BATT_STAT_NOT_PRESENT;
435 ACPI_SERIAL_END(cmbat);
436
437 return (0);
438 }
439
440 static void
441 acpi_cmbat_init_battery(void *arg)
442 {
443 struct acpi_cmbat_softc *sc;
444 int retry, valid;
445 device_t dev;
446
447 dev = (device_t)arg;
448 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
449 "battery initialization start\n");
450
451 /*
452 * Try repeatedly to get valid data from the battery. Since the
453 * embedded controller isn't always ready just after boot, we may have
454 * to wait a while.
455 */
456 for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10000)) {
457 /*
458 * Batteries on DOCK can be ejected w/ DOCK during retrying.
459 *
460 * If there is a valid softc pointer the device may be in
461 * attaching, attached or detaching state. If the state is
462 * different from attached retry getting the device state
463 * until it becomes stable. This solves a race if the ACPI
464 * notification handler is called during attach, because
465 * device_is_attached() doesn't return non-zero until after
466 * the attach code has been executed.
467 */
468 ACPI_SERIAL_BEGIN(cmbat);
469 sc = device_get_softc(dev);
470 if (sc == NULL) {
471 ACPI_SERIAL_END(cmbat);
472 return;
473 }
474
475 if (!acpi_BatteryIsPresent(dev) || !device_is_attached(dev)) {
476 ACPI_SERIAL_END(cmbat);
477 continue;
478 }
479
480 /*
481 * Only query the battery if this is the first try or the specific
482 * type of info is still invalid.
483 */
484 if (retry == 0 || !acpi_battery_bst_valid(&sc->bst)) {
485 timespecclear(&sc->bst_lastupdated);
486 acpi_cmbat_get_bst(dev);
487 }
488 if (retry == 0 || !acpi_battery_bif_valid(&sc->bif))
489 acpi_cmbat_get_bif(dev);
490
491 valid = acpi_battery_bst_valid(&sc->bst) &&
492 acpi_battery_bif_valid(&sc->bif);
493 ACPI_SERIAL_END(cmbat);
494
495 if (valid)
496 break;
497 }
498
499 if (retry == ACPI_CMBAT_RETRY_MAX) {
500 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
501 "battery initialization failed, giving up\n");
502 } else {
503 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
504 "battery initialization done, tried %d times\n", retry + 1);
505 }
506 }
Cache object: a9b5cab930387cb1cd2f628d2a8e6d58
|