>Number: 152792 >Category: kern >Synopsis: [patch] move temperature conversion macros to a common header >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Thu Dec 02 21:10:09 UTC 2010 >Closed-Date: >Last-Modified: >Originator: Mark Johnston >Release: >Organization: Sandvine Inc. >Environment: >Description: There are a few drivers that report system temperatures through sysctls using the "IK" format. This format expects values that are converted into multiples of 0.1 degrees Kelvin, and since most sensors report values in Celsius, they have to be converted. The drivers (acpi, coretemp and amdtemp) all have their own macros for doing this. This change puts a set of macros in sysctl.h for all drivers to use; moreover, they allow userspace programs to read temperature sysctls without having to redefine the macros yet again. >How-To-Repeat:
>Fix: Attached is a patch against HEAD (csup'ed about 2 days ago). Patch attached with submission follows: diff --git a/sys/dev/acpica/acpi_thermal.c b/sys/dev/acpica/acpi_thermal.c index ec4178e..f3a283c 100644 --- a/sys/dev/acpica/acpi_thermal.c +++ b/sys/dev/acpica/acpi_thermal.c @@ -54,9 +54,6 @@ __FBSDID("$FreeBSD: src/sys/dev/acpica/acpi_thermal.c,v 1.74 2010/06/11 19:27:21 #define _COMPONENT ACPI_THERMAL ACPI_MODULE_NAME("THERMAL") -#define TZ_ZEROC 2732 -#define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10) - #define TZ_NOTIFY_TEMPERATURE 0x80 /* Temperature changed. */ #define TZ_NOTIFY_LEVELS 0x81 /* Cooling levels changed. */ #define TZ_NOTIFY_DEVICES 0x82 /* Device lists changed. */ diff --git a/sys/dev/amdtemp/amdtemp.c b/sys/dev/amdtemp/amdtemp.c index 8ba46c7..0220b54 100644 --- a/sys/dev/amdtemp/amdtemp.c +++ b/sys/dev/amdtemp/amdtemp.c @@ -433,8 +433,6 @@ amdtemp_sysctl(SYSCTL_HANDLER_ARGS) return (error); } -#define AMDTEMP_ZERO_C_TO_K 2732 - static int32_t amdtemp_gettemp0f(device_t dev, amdsensor_t sensor) { @@ -468,7 +466,7 @@ amdtemp_gettemp0f(device_t dev, amdsensor_t sensor) pci_write_config(dev, AMDTEMP_THERMTP_STAT, cfg | sel, 1); /* CurTmp starts from -49C. */ - offset = AMDTEMP_ZERO_C_TO_K - 490; + offset = TZ_ZEROC - 490; /* Adjust offset if DiodeOffset is set and valid. */ temp = pci_read_config(dev, AMDTEMP_THERMTP_STAT, 4); @@ -497,7 +495,7 @@ amdtemp_gettemp(device_t dev, amdsensor_t sensor) int32_t diode_offset, offset; /* CurTmp starts from 0C. */ - offset = AMDTEMP_ZERO_C_TO_K; + offset = TZ_ZEROC; /* Adjust offset if DiodeOffset is set and valid. */ temp = pci_read_config(dev, AMDTEMP_THERMTP_STAT, 4); diff --git a/sys/dev/coretemp/coretemp.c b/sys/dev/coretemp/coretemp.c index 7cfc9c6..cb26b1b 100644 --- a/sys/dev/coretemp/coretemp.c +++ b/sys/dev/coretemp/coretemp.c @@ -48,8 +48,6 @@ __FBSDID("$FreeBSD: src/sys/dev/coretemp/coretemp.c,v 1.12 2010/08/04 00:25:13 d #include <machine/cputypes.h> #include <machine/md_var.h> -#define TZ_ZEROC 2732 - struct coretemp_softc { device_t sc_dev; int sc_tjmax; @@ -331,7 +329,7 @@ coretemp_get_temp_sysctl(SYSCTL_HANDLER_ARGS) device_t dev = (device_t) arg1; int temp; - temp = coretemp_get_temp(dev) * 10 + TZ_ZEROC; + temp = TZ_CEL_TO_KEL(coretemp_get_temp(dev)); return (sysctl_handle_int(oidp, &temp, 0, req)); } diff --git a/sys/sys/sysctl.h b/sys/sys/sysctl.h index eae9c10..3ffccf9 100644 --- a/sys/sys/sysctl.h +++ b/sys/sys/sysctl.h @@ -114,6 +114,19 @@ struct ctlname { */ #define CTL_AUTO_START 0x100 +/* + * Conversion between Celsius and Kelvin, for drivers that want to + * display a temperature reading through a sysctl. The IK sysctl format + * expects a value that's converted into multiples of 0.1 degrees Kelvin, + * so one multiplies a celsius reading by 10 and adds TZ_ZEROC to get the + * correct value. + */ +#define TZ_ZEROC 2732 +#define TZ_CTOKELV(x) ((x) * 10 + TZ_ZEROC) +#define TZ_KELVTOC_INT(x) (((x) - TZ_ZEROC) / 10) +#define TZ_KELVTOC_FRAC(x) abs(((x) - TZ_ZEROC) % 10) +#define TZ_KELVTOC(x) TZ_KELVTOC_INT(x), TZ_KELVTOC_FRAC(x) + #ifdef _KERNEL #define SYSCTL_HANDLER_ARGS struct sysctl_oid *oidp, void *arg1, int arg2, \ struct sysctl_req *req >Release-Note: >Audit-Trail: >Unformatted: _______________________________________________ freebsd-bugs@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-bugs To unsubscribe, send any mail to "freebsd-bugs-unsubscr...@freebsd.org"