sensor: Factor out helper functions.
authorSepherosa Ziehau <sephe@dragonflybsd.org>
Tue, 5 May 2015 14:13:22 +0000 (22:13 +0800)
committerSepherosa Ziehau <sephe@dragonflybsd.org>
Wed, 6 May 2015 01:15:43 +0000 (09:15 +0800)
sys/dev/misc/dimm/dimm.c
sys/dev/misc/ecc/ecc_e5.c
sys/dev/powermng/coretemp/coretemp.c
sys/dev/powermng/memtemp/memtemp_core.c
sys/dev/powermng/memtemp/memtemp_e5.c
sys/sys/sensors.h

index 12c6916..b3889e0 100644 (file)
@@ -210,6 +210,8 @@ dimm_set_temp_thresh(struct dimm_softc *sc, int hiwat, int lowat)
 void
 dimm_sensor_temp(struct dimm_softc *sc, struct ksensor *sens, int temp)
 {
+       enum sensor_status status;
+
        if (temp >= sc->dimm_temp_hiwat &&
            (sc->dimm_sens_taskflags & DIMM_SENS_TF_TEMP_CRIT) == 0) {
                char temp_str[16], data[64];
@@ -231,31 +233,31 @@ dimm_sensor_temp(struct dimm_softc *sc, struct ksensor *sens, int temp)
        }
 
        if (sc->dimm_sens_taskflags & DIMM_SENS_TF_TEMP_CRIT)
-               sens->status = SENSOR_S_CRIT;
+               status = SENSOR_S_CRIT;
        else
-               sens->status = SENSOR_S_OK;
-       sens->flags &= ~SENSOR_FINVALID;
-       sens->value = (temp * 1000000) + 273150000;
+               status = SENSOR_S_OK;
+       sensor_set_temp_degc(sens, temp, status);
 }
 
 void
 dimm_sensor_ecc_set(struct dimm_softc *sc, struct ksensor *sens,
     int ecc_cnt, boolean_t crit)
 {
+       enum sensor_status status;
+
+       sc->dimm_ecc_cnt = ecc_cnt;
        if (crit && (sc->dimm_sens_taskflags & DIMM_SENS_TF_ECC_CRIT) == 0) {
                /* TODO devctl(4) */
                sc->dimm_sens_taskflags |= DIMM_SENS_TF_ECC_CRIT;
        } else if (!crit && (sc->dimm_sens_taskflags & DIMM_SENS_TF_ECC_CRIT)) {
                sc->dimm_sens_taskflags &= ~DIMM_SENS_TF_ECC_CRIT;
        }
-       sc->dimm_ecc_cnt = ecc_cnt;
 
        if (sc->dimm_sens_taskflags & DIMM_SENS_TF_ECC_CRIT)
-               sens->status = SENSOR_S_CRIT;
+               status = SENSOR_S_CRIT;
        else
-               sens->status = SENSOR_S_OK;
-       sens->flags &= ~SENSOR_FINVALID;
-       sens->value = ecc_cnt;
+               status = SENSOR_S_OK;
+       sensor_set(sens, sc->dimm_ecc_cnt, status);
 }
 
 static void
index 9c56aa1..c64b586 100644 (file)
@@ -290,8 +290,8 @@ ecc_e5_attach(device_t dev)
                    "node%d chan%d DIMM%d ecc",
                    sc->ecc_node, sc->ecc_chan->chan_ext, dimm);
                sens->type = SENSOR_INTEGER;
+               sensor_set(sens, 0, SENSOR_S_OK);
                dimm_sensor_attach(dimm_sc->dimm_softc, sens);
-               dimm_sensor_ecc_set(dimm_sc->dimm_softc, sens, 0, FALSE);
 
                TAILQ_INSERT_TAIL(&sc->ecc_dimm, dimm_sc, dimm_link);
 
index 2698def..0e13c4b 100644 (file)
@@ -165,34 +165,17 @@ static devclass_t coretemp_devclass;
 DRIVER_MODULE(coretemp, cpu, coretemp_driver, coretemp_devclass, NULL, NULL);
 MODULE_VERSION(coretemp, 1);
 
-static __inline void
-coretemp_sensor_set_invalid(struct ksensor *sens)
-{
-       sens->status = SENSOR_S_UNSPEC;
-       sens->flags &= ~SENSOR_FUNKNOWN;
-       sens->flags |= SENSOR_FINVALID;
-       sens->value = 0;
-}
-
-static __inline void
-coretemp_sensor_set_unknown(struct ksensor *sens)
-{
-       sens->status = SENSOR_S_UNSPEC;
-       sens->flags &= ~SENSOR_FINVALID;
-       sens->flags |= SENSOR_FUNKNOWN;
-       sens->value = 0;
-}
-
 static __inline void
 coretemp_sensor_set(struct ksensor *sens, const struct coretemp_softc *sc,
     uint32_t crit_flag, int temp)
 {
+       enum sensor_status status;
+
        if (sc->sc_flags & crit_flag)
-               sens->status = SENSOR_S_CRIT;
+               status = SENSOR_S_CRIT;
        else
-               sens->status = SENSOR_S_OK;
-       sens->flags &= ~(SENSOR_FINVALID | SENSOR_FUNKNOWN);
-       sens->value = temp * 1000000 + 273150000;
+               status = SENSOR_S_OK;
+       sensor_set_temp_degc(sens, temp, status);
 }
 
 static void
@@ -398,7 +381,7 @@ coretemp_attach(device_t dev)
                    "node%d core%d", get_chip_ID(cpu),
                    get_core_number_within_chip(cpu));
                csens->c_sens.type = SENSOR_TEMP;
-               coretemp_sensor_set_unknown(&csens->c_sens);
+               sensor_set_unknown(&csens->c_sens);
                sensor_attach(&csens->c_sensdev, &csens->c_sens);
                sensordev_install(&csens->c_sensdev);
 
@@ -446,7 +429,7 @@ coretemp_attach(device_t dev)
                            sizeof(csens->c_sens.desc), "node%d",
                            get_chip_ID(sc->sc_cpu));
                        csens->c_sens.type = SENSOR_TEMP;
-                       coretemp_sensor_set_unknown(&csens->c_sens);
+                       sensor_set_unknown(&csens->c_sens);
                        sensor_attach(&csens->c_sensdev, &csens->c_sens);
                        sensordev_install(&csens->c_sensdev);
                }
@@ -637,7 +620,7 @@ coretemp_sensor_update(struct coretemp_softc *sc, int temp)
                /* No updates; keep the previous value */
        } else if (temp == CORETEMP_TEMP_INVALID) {
                for (i = 0; i < sc->sc_nsens; ++i)
-                       coretemp_sensor_set_invalid(&sc->sc_sens[i].c_sens);
+                       sensor_set_invalid(&sc->sc_sens[i].c_sens);
        } else {
                for (i = 0; i < sc->sc_nsens; ++i) {
                        coretemp_sensor_set(&sc->sc_sens[i].c_sens, sc,
@@ -653,7 +636,7 @@ coretemp_pkg_sensor_update(struct coretemp_softc *sc, int temp)
        if (temp == CORETEMP_TEMP_NOUPDATE) {
                /* No updates; keep the previous value */
        } else if (temp == CORETEMP_TEMP_INVALID) {
-               coretemp_sensor_set_invalid(&sc->sc_pkg_sens->c_sens);
+               sensor_set_invalid(&sc->sc_pkg_sens->c_sens);
        } else {
                coretemp_sensor_set(&sc->sc_pkg_sens->c_sens, sc,
                    CORETEMP_FLAG_PKGCRIT, temp);
index f20efd6..8e0a24d 100644 (file)
@@ -232,6 +232,7 @@ memtemp_core_dimm_attach(struct memtemp_core_softc *sc, int chan, int dimm_id,
        ksnprintf(sens->desc, sizeof(sens->desc), "chan%d DIMM%d temp",
            chan, dimm_id);
        sens->type = SENSOR_TEMP;
+       sensor_set_unknown(sens);
        dimm_sensor_attach(dimm_sc->dimm_softc, sens);
        sensor_task_register(dimm_sc, memtemp_core_sensor_task, 5);
 
index 7fca55f..ea0fde9 100644 (file)
@@ -305,6 +305,7 @@ memtemp_e5_attach(device_t dev)
                    "node%d chan%d DIMM%d temp",
                    sc->temp_node, sc->temp_chan->chan_ext, dimm);
                sens->type = SENSOR_TEMP;
+               sensor_set_unknown(sens);
                dimm_sensor_attach(dimm_sc->dimm_softc, sens);
                sensor_task_register(dimm_sc, memtemp_e5_sensor_task, 5);
 
index 50f2786..d9984de 100644 (file)
@@ -121,6 +121,7 @@ struct sensordev {
 #define MAXSENSORDEVICES 32
 
 #ifdef _KERNEL
+
 #include <sys/queue.h>
 #include <sys/sysctl.h>
 
@@ -162,6 +163,38 @@ void               sensor_detach(struct ksensordev *, struct ksensor *);
 void           sensor_task_register(void *, void (*)(void *), int);
 void           sensor_task_unregister(void *);
 
+static __inline void
+sensor_set_invalid(struct ksensor *sens)
+{
+       sens->status = SENSOR_S_UNSPEC;
+       sens->flags &= ~(SENSOR_FUNKNOWN | SENSOR_FINVALID);
+       sens->flags |= SENSOR_FINVALID;
+       sens->value = 0;
+}
+
+static __inline void
+sensor_set_unknown(struct ksensor *sens)
+{
+       sens->status = SENSOR_S_UNKNOWN;
+       sens->flags &= ~(SENSOR_FUNKNOWN | SENSOR_FINVALID);
+       sens->flags |= SENSOR_FUNKNOWN;
+       sens->value = 0;
+}
+
+static __inline void
+sensor_set(struct ksensor *sens, int val, enum sensor_status status)
+{
+       sens->status = status;
+       sens->flags &= ~(SENSOR_FUNKNOWN | SENSOR_FINVALID);
+       sens->value = val;
+}
+
+static __inline void
+sensor_set_temp_degc(struct ksensor *sens, int degc, enum sensor_status status)
+{
+       sensor_set(sens, (degc * 1000000) + 273150000, status);
+}
+
 #endif /* _KERNEL */
 
 #endif /* !_SYS_SENSORS_H_ */