First, I think the BatteryPresent check has a signedness problem.  Second, I 
think that it should check for sensor validity too, so it doesn’t use stale 
values if BatteryPresent somehow goes straight from present to invalid.

This diff should fix both things, in theory.  Compiled and running without 
adverse effects.  I did not pull apart my UPS while it’s actively running to 
confirm, though.

--david

Index: upd.c
===================================================================
RCS file: /cvs/src/sys/dev/usb/upd.c,v
retrieving revision 1.12
diff -u -p -r1.12 upd.c
--- upd.c       11 Dec 2014 18:50:32 -0000      1.12
+++ upd.c       12 Dec 2014 02:11:09 -0000
@@ -322,12 +322,14 @@ upd_update_sensors(struct upd_softc *sc,
     int repid)
 {
        struct upd_sensor       *sensor;
-       ulong                   hdata, batpres;
+       ulong                   hdata;
        ulong                   adjust;
-       int                     i;
+       int                     i, batpres = 0;
 
        sensor = upd_lookup_sensor(sc, HUP_BATTERY, HUB_BATTERY_PRESENT);
-       batpres = sensor ? sensor->ksensor.value : -1;
+       if (sensor && !(sensor->ksensor.flags & SENSOR_FINVALID) &&
+           sensor->ksensor.value)
+               batpres = 1;
 
        for (i = 0; i < sc->sc_num_sensors; i++) {
                sensor = &sc->sc_sensors[i];
@@ -336,7 +338,7 @@ upd_update_sensors(struct upd_softc *sc,
 
                /* invalidate battery dependent sensors */
                if (HID_GET_USAGE_PAGE(sensor->hitem.usage) == HUP_BATTERY &&
-                   batpres <= 0) {
+                   !batpres) {
                        /* exception to the battery sensor itself */
                        if (HID_GET_USAGE(sensor->hitem.usage) !=
                            HUB_BATTERY_PRESENT) {


Reply via email to