acpi_cmbat.c (85325) | acpi_cmbat.c (85729) |
---|---|
1/*- 2 * Copyright (c) 2000 Munehiro Matsuda 3 * Copyright (c) 2000 Takanori Watanabe 4 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions --- 11 unchanged lines hidden (view full) --- 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * | 1/*- 2 * Copyright (c) 2000 Munehiro Matsuda 3 * Copyright (c) 2000 Takanori Watanabe 4 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions --- 11 unchanged lines hidden (view full) --- 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * |
28 * $FreeBSD: head/sys/dev/acpica/acpi_cmbat.c 85325 2001-10-22 18:01:37Z iwasaki $ | 28 * $FreeBSD: head/sys/dev/acpica/acpi_cmbat.c 85729 2001-10-30 14:24:26Z iwasaki $ |
29 */ 30 31#include "opt_acpi.h" 32#include <sys/param.h> 33#include <sys/kernel.h> 34#include <sys/bus.h> 35#include <sys/ioccom.h> 36#include <sys/conf.h> 37 38#include <machine/bus.h> 39#include <machine/resource.h> 40#include <sys/rman.h> 41#include <sys/malloc.h> 42 43#include "acpi.h" 44 45#include <dev/acpica/acpivar.h> 46#include <dev/acpica/acpiio.h> 47 | 29 */ 30 31#include "opt_acpi.h" 32#include <sys/param.h> 33#include <sys/kernel.h> 34#include <sys/bus.h> 35#include <sys/ioccom.h> 36#include <sys/conf.h> 37 38#include <machine/bus.h> 39#include <machine/resource.h> 40#include <sys/rman.h> 41#include <sys/malloc.h> 42 43#include "acpi.h" 44 45#include <dev/acpica/acpivar.h> 46#include <dev/acpica/acpiio.h> 47 |
48MALLOC_DEFINE(M_ACPICMBAT, "acpicmbat", "ACPI control method battery data"); 49 |
|
48#define CMBAT_POLLRATE (60 * hz) 49 50/* 51 * Hooks for the ACPI CA debugging infrastructure 52 */ 53#define _COMPONENT ACPI_BATTERY 54MODULE_NAME("BATTERY") 55 --- 65 unchanged lines hidden (view full) --- 121 strncpy(dest, tmp->Buffer.Pointer, length); \ 122 break; \ 123 default: \ 124 goto label; \ 125 } \ 126 dest[sizeof(dest)-1] = '\0'; \ 127} while(0) 128 | 50#define CMBAT_POLLRATE (60 * hz) 51 52/* 53 * Hooks for the ACPI CA debugging infrastructure 54 */ 55#define _COMPONENT ACPI_BATTERY 56MODULE_NAME("BATTERY") 57 --- 65 unchanged lines hidden (view full) --- 123 strncpy(dest, tmp->Buffer.Pointer, length); \ 124 break; \ 125 default: \ 126 goto label; \ 127 } \ 128 dest[sizeof(dest)-1] = '\0'; \ 129} while(0) 130 |
131#define CMBAT_DPRINT(dev, x...) do { \ 132 if (acpi_get_verbose(acpi_device_get_parent_softc(dev))) \ 133 device_printf(dev, x); \ 134} while (0) 135 |
|
129/* 130 * Poll the battery info. 131 */ 132static void 133acpi_cmbat_timeout(void *context) 134{ 135 device_t dev; 136 struct acpi_cmbat_softc *sc; 137 138 dev = (device_t)context; 139 sc = device_get_softc(dev); 140 141 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev); 142 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE); 143} 144 | 136/* 137 * Poll the battery info. 138 */ 139static void 140acpi_cmbat_timeout(void *context) 141{ 142 device_t dev; 143 struct acpi_cmbat_softc *sc; 144 145 dev = (device_t)context; 146 sc = device_get_softc(dev); 147 148 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev); 149 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE); 150} 151 |
145#define BATTERY_INFO_EXPIRE 5 | |
146static __inline int 147acpi_cmbat_info_expired(struct timespec *lastupdated) 148{ 149 struct timespec curtime; 150 151 if (lastupdated == NULL) { 152 return (1); 153 } 154 155 if (!timespecisset(lastupdated)) { 156 return (1); 157 } 158 159 getnanotime(&curtime); 160 timespecsub(&curtime, lastupdated); | 152static __inline int 153acpi_cmbat_info_expired(struct timespec *lastupdated) 154{ 155 struct timespec curtime; 156 157 if (lastupdated == NULL) { 158 return (1); 159 } 160 161 if (!timespecisset(lastupdated)) { 162 return (1); 163 } 164 165 getnanotime(&curtime); 166 timespecsub(&curtime, lastupdated); |
161 return ((curtime.tv_sec < 0 || curtime.tv_sec > BATTERY_INFO_EXPIRE)); | 167 return ((curtime.tv_sec < 0 || curtime.tv_sec > acpi_battery_get_info_expire())); |
162} 163 164 165static __inline void 166acpi_cmbat_info_updated(struct timespec *lastupdated) 167{ 168 169 if (lastupdated != NULL) { --- 9 unchanged lines hidden (view full) --- 179 ACPI_STATUS as; 180 ACPI_OBJECT *res, *tmp; 181 ACPI_HANDLE h = acpi_get_handle(dev); 182 183 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) { 184 return; 185 } 186 | 168} 169 170 171static __inline void 172acpi_cmbat_info_updated(struct timespec *lastupdated) 173{ 174 175 if (lastupdated != NULL) { --- 9 unchanged lines hidden (view full) --- 185 ACPI_STATUS as; 186 ACPI_OBJECT *res, *tmp; 187 ACPI_HANDLE h = acpi_get_handle(dev); 188 189 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) { 190 return; 191 } 192 |
193 untimeout(acpi_cmbat_timeout, (caddr_t)dev, sc->cmbat_timeout); |
|
187retry: 188 if (sc->bst_buffer.Length == 0) { | 194retry: 195 if (sc->bst_buffer.Length == 0) { |
189 sc->bst_buffer.Pointer = NULL; | 196 if (sc->bst_buffer.Pointer != NULL) { 197 free(sc->bst_buffer.Pointer, M_ACPICMBAT); 198 sc->bst_buffer.Pointer = NULL; 199 } |
190 as = AcpiEvaluateObject(h, "_BST", NULL, &sc->bst_buffer); 191 if (as != AE_BUFFER_OVERFLOW){ | 200 as = AcpiEvaluateObject(h, "_BST", NULL, &sc->bst_buffer); 201 if (as != AE_BUFFER_OVERFLOW){ |
192 device_printf(dev, "CANNOT FOUND _BST (%d)\n", as); 193 return; | 202 CMBAT_DPRINT(dev, "CANNOT FOUND _BST - %s\n", 203 AcpiFormatException(as)); 204 goto end; |
194 } 195 | 205 } 206 |
196 sc->bst_buffer.Pointer = malloc(sc->bst_buffer.Length, M_DEVBUF, M_NOWAIT); | 207 sc->bst_buffer.Pointer = malloc(sc->bst_buffer.Length, M_ACPICMBAT, M_NOWAIT); |
197 if (sc->bst_buffer.Pointer == NULL) { 198 device_printf(dev,"malloc failed"); | 208 if (sc->bst_buffer.Pointer == NULL) { 209 device_printf(dev,"malloc failed"); |
199 return; | 210 goto end; |
200 } 201 } 202 203 bzero(sc->bst_buffer.Pointer, sc->bst_buffer.Length); 204 as = AcpiEvaluateObject(h, "_BST", NULL, &sc->bst_buffer); 205 206 if (as == AE_BUFFER_OVERFLOW){ 207 if (sc->bst_buffer.Pointer){ | 211 } 212 } 213 214 bzero(sc->bst_buffer.Pointer, sc->bst_buffer.Length); 215 as = AcpiEvaluateObject(h, "_BST", NULL, &sc->bst_buffer); 216 217 if (as == AE_BUFFER_OVERFLOW){ 218 if (sc->bst_buffer.Pointer){ |
208 free(sc->bst_buffer.Pointer, M_DEVBUF); | 219 free(sc->bst_buffer.Pointer, M_ACPICMBAT); 220 sc->bst_buffer.Pointer = NULL; |
209 } | 221 } |
210 device_printf(dev, "bst size changed to %d\n", sc->bst_buffer.Length); | 222 CMBAT_DPRINT(dev, "bst size changed to %d\n", sc->bst_buffer.Length); |
211 sc->bst_buffer.Length = 0; 212 goto retry; 213 } else if (as != AE_OK){ | 223 sc->bst_buffer.Length = 0; 224 goto retry; 225 } else if (as != AE_OK){ |
214 device_printf(dev, "CANNOT FOUND _BST (%d)\n", as); 215 return; | 226 CMBAT_DPRINT(dev, "CANNOT FOUND _BST - %s\n", 227 AcpiFormatException(as)); 228 goto end; |
216 } 217 218 res = (ACPI_OBJECT *)sc->bst_buffer.Pointer; 219 220 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 4)) { | 229 } 230 231 res = (ACPI_OBJECT *)sc->bst_buffer.Pointer; 232 233 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 4)) { |
221 device_printf(dev, "Battery status corrupted\n"); 222 return; | 234 CMBAT_DPRINT(dev, "Battery status corrupted\n"); 235 goto end; |
223 } 224 225 PKG_GETINT(res, tmp, 0, sc->bst.state, end); 226 PKG_GETINT(res, tmp, 1, sc->bst.rate, end); 227 PKG_GETINT(res, tmp, 2, sc->bst.cap, end); 228 PKG_GETINT(res, tmp, 3, sc->bst.volt, end); 229 acpi_cmbat_info_updated(&sc->bst_lastupdated); 230end: | 236 } 237 238 PKG_GETINT(res, tmp, 0, sc->bst.state, end); 239 PKG_GETINT(res, tmp, 1, sc->bst.rate, end); 240 PKG_GETINT(res, tmp, 2, sc->bst.cap, end); 241 PKG_GETINT(res, tmp, 3, sc->bst.volt, end); 242 acpi_cmbat_info_updated(&sc->bst_lastupdated); 243end: |
244 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE); |
|
231} 232 233static void 234acpi_cmbat_get_bif(void *context) 235{ 236 device_t dev = context; 237 struct acpi_cmbat_softc *sc = device_get_softc(dev); 238 ACPI_STATUS as; 239 ACPI_HANDLE h = acpi_get_handle(dev); 240 ACPI_OBJECT *res, *tmp; 241 242 if (!acpi_cmbat_info_expired(&sc->bif_lastupdated)) { 243 return; 244 } 245 246 untimeout(acpi_cmbat_timeout, (caddr_t)dev, sc->cmbat_timeout); 247retry: 248 if (sc->bif_buffer.Length == 0) { | 245} 246 247static void 248acpi_cmbat_get_bif(void *context) 249{ 250 device_t dev = context; 251 struct acpi_cmbat_softc *sc = device_get_softc(dev); 252 ACPI_STATUS as; 253 ACPI_HANDLE h = acpi_get_handle(dev); 254 ACPI_OBJECT *res, *tmp; 255 256 if (!acpi_cmbat_info_expired(&sc->bif_lastupdated)) { 257 return; 258 } 259 260 untimeout(acpi_cmbat_timeout, (caddr_t)dev, sc->cmbat_timeout); 261retry: 262 if (sc->bif_buffer.Length == 0) { |
249 sc->bif_buffer.Pointer = NULL; | 263 if (sc->bif_buffer.Pointer != NULL) { 264 free(sc->bif_buffer.Pointer, M_ACPICMBAT); 265 sc->bif_buffer.Pointer = NULL; 266 } |
250 as = AcpiEvaluateObject(h, "_BIF", NULL, &sc->bif_buffer); 251 if (as != AE_BUFFER_OVERFLOW){ | 267 as = AcpiEvaluateObject(h, "_BIF", NULL, &sc->bif_buffer); 268 if (as != AE_BUFFER_OVERFLOW){ |
252 device_printf(dev, "CANNOT FOUND _BIF (%d)\n", as); | 269 CMBAT_DPRINT(dev, "CANNOT FOUND _BIF - %s\n", 270 AcpiFormatException(as)); |
253 goto end; 254 } 255 | 271 goto end; 272 } 273 |
256 sc->bif_buffer.Pointer = malloc(sc->bif_buffer.Length, M_DEVBUF, M_NOWAIT); | 274 sc->bif_buffer.Pointer = malloc(sc->bif_buffer.Length, M_ACPICMBAT, M_NOWAIT); |
257 if (sc->bif_buffer.Pointer == NULL) { 258 device_printf(dev,"malloc failed"); 259 goto end; 260 } 261 } 262 263 bzero(sc->bif_buffer.Pointer, sc->bif_buffer.Length); 264 as = AcpiEvaluateObject(h, "_BIF", NULL, &sc->bif_buffer); 265 266 if (as == AE_BUFFER_OVERFLOW){ 267 if (sc->bif_buffer.Pointer){ | 275 if (sc->bif_buffer.Pointer == NULL) { 276 device_printf(dev,"malloc failed"); 277 goto end; 278 } 279 } 280 281 bzero(sc->bif_buffer.Pointer, sc->bif_buffer.Length); 282 as = AcpiEvaluateObject(h, "_BIF", NULL, &sc->bif_buffer); 283 284 if (as == AE_BUFFER_OVERFLOW){ 285 if (sc->bif_buffer.Pointer){ |
268 free(sc->bif_buffer.Pointer, M_DEVBUF); | 286 free(sc->bif_buffer.Pointer, M_ACPICMBAT); 287 sc->bif_buffer.Pointer = NULL; |
269 } | 288 } |
270 device_printf(dev, "bif size changed to %d\n", sc->bif_buffer.Length); | 289 CMBAT_DPRINT(dev, "bif size changed to %d\n", sc->bif_buffer.Length); |
271 sc->bif_buffer.Length = 0; 272 goto retry; 273 } else if (as != AE_OK){ | 290 sc->bif_buffer.Length = 0; 291 goto retry; 292 } else if (as != AE_OK){ |
274 device_printf(dev, "CANNOT FOUND _BIF (%d)\n", as); | 293 CMBAT_DPRINT(dev, "CANNOT FOUND _BIF - %s\n", 294 AcpiFormatException(as)); |
275 goto end; 276 } 277 278 res = (ACPI_OBJECT *)sc->bif_buffer.Pointer; 279 280 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 13)) { | 295 goto end; 296 } 297 298 res = (ACPI_OBJECT *)sc->bif_buffer.Pointer; 299 300 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 13)) { |
281 device_printf(dev, "Battery info corrupted\n"); | 301 CMBAT_DPRINT(dev, "Battery info corrupted\n"); |
282 goto end; 283 } 284 285 PKG_GETINT(res, tmp, 0, sc->bif.unit, end); 286 PKG_GETINT(res, tmp, 1, sc->bif.dcap, end); 287 PKG_GETINT(res, tmp, 2, sc->bif.lfcap, end); 288 PKG_GETINT(res, tmp, 3, sc->bif.btech, end); 289 PKG_GETINT(res, tmp, 4, sc->bif.dvol, end); --- 82 unchanged lines hidden (view full) --- 372 if ((error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT, 373 acpi_cmbat_units)) != 0) { 374 return (error); 375 } 376 377 acpi_cmbat_units++; 378 timespecclear(&acpi_cmbat_info_lastupdated); 379 | 302 goto end; 303 } 304 305 PKG_GETINT(res, tmp, 0, sc->bif.unit, end); 306 PKG_GETINT(res, tmp, 1, sc->bif.dcap, end); 307 PKG_GETINT(res, tmp, 2, sc->bif.lfcap, end); 308 PKG_GETINT(res, tmp, 3, sc->bif.btech, end); 309 PKG_GETINT(res, tmp, 4, sc->bif.dvol, end); --- 82 unchanged lines hidden (view full) --- 392 if ((error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT, 393 acpi_cmbat_units)) != 0) { 394 return (error); 395 } 396 397 acpi_cmbat_units++; 398 timespecclear(&acpi_cmbat_info_lastupdated); 399 |
380 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev); | |
381 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE); 382 return(0); 383} 384 385static int 386acpi_cmbat_resume(device_t dev) 387{ 388 --- 86 unchanged lines hidden (view full) --- 475 476 cap = min = -1; 477 batt_stat = ACPI_BATT_STAT_NOT_PRESENT; 478 error = 0; 479 480 /* Allocate array of softc pointers */ 481 if (bat_units != acpi_cmbat_units) { 482 if (bat != NULL) { | 400 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE); 401 return(0); 402} 403 404static int 405acpi_cmbat_resume(device_t dev) 406{ 407 --- 86 unchanged lines hidden (view full) --- 494 495 cap = min = -1; 496 batt_stat = ACPI_BATT_STAT_NOT_PRESENT; 497 error = 0; 498 499 /* Allocate array of softc pointers */ 500 if (bat_units != acpi_cmbat_units) { 501 if (bat != NULL) { |
483 free(bat, M_DEVBUF); | 502 free(bat, M_ACPICMBAT); 503 bat = NULL; |
484 } 485 bat_units = 0; 486 } 487 if (bat == NULL) { 488 bat_units = acpi_cmbat_units; 489 bat = malloc(sizeof(struct acpi_cmbat_softc *) * bat_units, | 504 } 505 bat_units = 0; 506 } 507 if (bat == NULL) { 508 bat_units = acpi_cmbat_units; 509 bat = malloc(sizeof(struct acpi_cmbat_softc *) * bat_units, |
490 M_DEVBUF, M_NOWAIT); | 510 M_ACPICMBAT, M_NOWAIT); |
491 if (bat == NULL) { 492 error = ENOMEM; 493 goto out; 494 } 495 496 /* Collect softc pointers */ 497 for (i = 0; i < acpi_cmbat_units; i++) { 498 if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) { --- 156 unchanged lines hidden --- | 511 if (bat == NULL) { 512 error = ENOMEM; 513 goto out; 514 } 515 516 /* Collect softc pointers */ 517 for (i = 0; i < acpi_cmbat_units; i++) { 518 if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) { --- 156 unchanged lines hidden --- |