[RFC] lm75 kernel driver and bsnmp module

Luiz Otavio O Souza lists.br at gmail.com
Thu Mar 27 14:34:28 UTC 2014


On 18 March 2014 11:26, Warner Losh wrote:
>
> On Mar 17, 2014, at 8:11 AM, Luiz Otavio O Souza wrote:
>
>> <lm75.diff>
>
> +#ifdef FDT
> +static struct ofw_compat_data compat_data[] = {
> +       { "freebsd,lm75",       HWTYPE_LM75 },
> +       { "freebsd,lm75a",      HWTYPE_LM75A },
> +       { NULL,                 HWTYPE_NONE },
> +};
> +#endif
>
> Is there a FDT standard here? It seems like they would fall under the
> "national,lm75" bindings that are defined in the "standard" FDT spec. You
> can find it listed in the 'device-tree/Bindings/i2c/trivial-devices.txt' file in
> Linux, or in a similar location in the device-tree vendor tree in FreeBSD.

Thanks for pointing it, i wasn't aware of this 'standard' spec.

>
> It isn't clear that both devices can't be handled with the same driver, since
> the extra bits are supposed to be clear and so the extra if's here:
>
> +       if (sc->sc_hwtype == HWTYPE_LM75A) {
> +               if (buf & LM75_0125C)
> +                       t += 125;
> +               if (buf & LM75_0250C)
> +                       t += 250;
> +       }
> +       if (buf & LM75_0500C)
> +               t += 500;
>
> which just leaves device type reporting. The only reason I mention is is that
> Linux doesn't seem to differentiate the two at the FDT level.

Yes it is not done at the FDT level, i'm not familiar with linux here,
but looks like you can specify the device type at lm-sensors layer.

With this they support all the lm75 clones and similar ICs in the same
driver, but only for the original lm75s they have a auto-detect
feature which differentiate the two versions of lm75.

I'll try to follow linux here, using the standard 'national,lm75' and
auto-detect the chip type as it seems to be quite straightforward.

>
> +       sc->sc_hwtype = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
>
> I don't think that the NULL binding is guaranteed to work that way, so
> I'd suggest dropping the NULL line above and testing explicitly for NULL
> here instead.

ofw_bus_search_compatible() will loop over the compat_data entries
until it find the NULL entry and then return.  Dropping the NULL line
would make it loop indefinitely.

>
> Finally, is pausing for hz on read/write errors the right thing to do? Seems like a very
> long time...

Yes, that was wrong and as i never seen any fail at that point i
removed all the retry code. I2C transfers seems to be quite reliable
on the systems i've been working.

New patch attached. I've changed the compat string to the standard
'national,lm75', removed the retry code and add and auto-detect
routine to identify the LM75 model. It seems to work fine (i have a
test-bed with two lm75 and one lm75a on the same i2c bus).

Thank you for your comments.

Luiz
-------------- next part --------------
Index: sys/conf/files
===================================================================
--- sys/conf/files	(revision 263740)
+++ sys/conf/files	(working copy)
@@ -1445,6 +1445,7 @@
 dev/iicbus/iicsmb.c		optional iicsmb				\
 	dependency	"iicbus_if.h"
 dev/iicbus/iicoc.c		optional iicoc
+dev/iicbus/lm75.c		optional lm75
 dev/iicbus/pcf8563.c		optional pcf8563
 dev/iicbus/s35390a.c		optional s35390a
 dev/iir/iir.c			optional iir
--- /dev/null	2014-03-27 10:47:58.000000000 -0300
+++ sys/dev/iicbus/lm75.c	2014-03-27 10:53:08.231187090 -0300
@@ -0,0 +1,582 @@
+/*-
+ * Copyright (c) 2010 Andreas Tobler.
+ * Copyright (c) 2013-2014 Luiz Otavio O Souza <loos at freebsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "opt_platform.h"
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/endian.h>
+#include <sys/kernel.h>
+#include <sys/module.h>
+#include <sys/sysctl.h>
+#include <sys/systm.h>
+
+#include <machine/bus.h>
+
+#include <dev/iicbus/iicbus.h>
+#include <dev/iicbus/iiconf.h>
+
+#ifdef FDT
+#include <dev/ofw/openfirm.h>
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+#endif
+
+/* LM75 registers. */
+#define	LM75_TEMP	0x0
+#define	LM75_CONF	0x1
+#define	LM75_CONF_FSHIFT	3
+#define	LM75_CONF_FAULT		0x18
+#define	LM75_CONF_POL		0x04
+#define	LM75_CONF_MODE		0x02
+#define	LM75_CONF_SHUTD		0x01
+#define	LM75_CONF_MASK		0x1f
+#define	LM75_THYST	0x2
+#define	LM75_TOS	0x3
+#define	LM75_ID		0x7
+
+/* LM75 constants. */
+#define	LM75_TEST_PATTERN	0xa
+#define	LM75_MIN_TEMP		-55
+#define	LM75_MAX_TEMP		125
+#define	LM75_0500C		0x80
+#define	LM75_0250C		0x40
+#define	LM75_0125C		0x20
+#define	LM75_MSB		0x8000
+#define	LM75_NEG_BIT		LM75_MSB
+#define	TZ_ZEROC		2732
+
+/* LM75 supported models. */
+#define	HWTYPE_LM75		1
+#define	HWTYPE_LM75A		2
+
+/* Regular bus attachment functions */
+static int  lm75_probe(device_t);
+static int  lm75_attach(device_t);
+
+struct lm75_softc {
+	device_t		sc_dev;
+	struct intr_config_hook enum_hook;
+	int32_t			sc_hwtype;
+	uint32_t		sc_addr;
+	uint32_t		sc_conf;
+};
+
+static int lm75_faults[4] = { 1, 2, 4, 6 };
+
+/* Utility functions */
+static int  lm75_conf_read(struct lm75_softc *);
+static int  lm75_conf_write(struct lm75_softc *);
+static int  lm75_temp_read(struct lm75_softc *, uint8_t, int *);
+static int  lm75_temp_write(struct lm75_softc *, uint8_t, int);
+static void lm75_start(void *);
+static int  lm75_read(device_t, uint32_t, uint8_t, uint8_t *, size_t);
+static int  lm75_write(device_t, uint32_t, uint8_t *, size_t);
+static int  lm75_str_mode(char *);
+static int  lm75_str_pol(char *);
+static int  lm75_temp_sysctl(SYSCTL_HANDLER_ARGS);
+static int  lm75_faults_sysctl(SYSCTL_HANDLER_ARGS);
+static int  lm75_mode_sysctl(SYSCTL_HANDLER_ARGS);
+static int  lm75_pol_sysctl(SYSCTL_HANDLER_ARGS);
+static int  lm75_shutdown_sysctl(SYSCTL_HANDLER_ARGS);
+
+static device_method_t  lm75_methods[] = {
+	/* Device interface */
+	DEVMETHOD(device_probe,		lm75_probe),
+	DEVMETHOD(device_attach,	lm75_attach),
+
+	DEVMETHOD_END
+};
+
+static driver_t lm75_driver = {
+	"lm75",
+	lm75_methods,
+	sizeof(struct lm75_softc)
+};
+
+static devclass_t lm75_devclass;
+
+DRIVER_MODULE(lm75, iicbus, lm75_driver, lm75_devclass, 0, 0);
+
+static int
+lm75_read(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data, size_t len)
+{
+	struct iic_msg msg[2] = {
+	    { addr, IIC_M_WR | IIC_M_NOSTOP, 1, &reg },
+	    { addr, IIC_M_RD, len, data },
+	};
+
+	if (iicbus_transfer(dev, msg, 2) != 0)
+		return (-1);
+
+	return (0);
+}
+
+static int
+lm75_write(device_t dev, uint32_t addr, uint8_t *data, size_t len)
+{
+	struct iic_msg msg[1] = {
+	    { addr, IIC_M_WR, len, data },
+	};
+
+	if (iicbus_transfer(dev, msg, 1) != 0)
+		return (-1);
+
+	return (0);
+}
+
+static int
+lm75_probe(device_t dev)
+{
+	struct lm75_softc *sc;
+
+	sc = device_get_softc(dev);
+	sc->sc_hwtype = HWTYPE_LM75;
+#ifdef FDT
+	if (!ofw_bus_is_compatible(dev, "national,lm75"))
+		return (ENXIO);
+#endif
+	device_set_desc(dev, "LM75 temperature sensor");
+
+	return (BUS_PROBE_GENERIC);
+}
+
+static int
+lm75_attach(device_t dev)
+{
+	struct lm75_softc *sc;
+
+	sc = device_get_softc(dev);
+	sc->sc_dev = dev;
+	sc->sc_addr = iicbus_get_addr(dev);
+
+	sc->enum_hook.ich_func = lm75_start;
+	sc->enum_hook.ich_arg = dev;
+
+	/*
+	 * We have to wait until interrupts are enabled.  Usually I2C read
+	 * and write only works when the interrupts are available.
+	 */
+	if (config_intrhook_establish(&sc->enum_hook) != 0)
+		return (ENOMEM);
+
+	return (0);
+}
+
+static int
+lm75_type_detect(struct lm75_softc *sc)
+{
+	int i, lm75a;
+        uint8_t buf8[2], conf;
+
+	/* Save the contents of the configuration register. */
+	if (lm75_read(sc->sc_dev, sc->sc_addr, LM75_CONF, buf8, 1) < 0)
+		return (-1);
+	conf = buf8[0];
+
+	/*
+	 * Just write some pattern we can verify later at configuration
+	 * register.
+	 */
+	buf8[0] = LM75_CONF;
+	buf8[1] = LM75_TEST_PATTERN;
+	if (lm75_write(sc->sc_dev, sc->sc_addr, buf8, 2) < 0)
+		return (-1);
+
+	/*
+	 * Read the configuration register again and check for our test
+	 * pattern.
+	 */
+	if (lm75_read(sc->sc_dev, sc->sc_addr, LM75_CONF, buf8, 1) < 0)
+		return (-1);
+	if (buf8[0] != LM75_TEST_PATTERN)
+		return (-1);
+
+	/*
+	 * Read from nonexistent registers (0x4 ~ 0x6).
+	 * LM75A always return 0xff for nonexistent registers.
+	 * LM75 will return the last read value - our test pattern written to
+	 * configuration register.
+	 */
+	lm75a = 0;
+	for (i = 4; i <= 6; i++) {
+		if (lm75_read(sc->sc_dev, sc->sc_addr, i, buf8, 1) < 0)
+			return (-1);
+		if (buf8[0] != LM75_TEST_PATTERN && buf8[0] != 0xff)
+			return (-1);
+		if (buf8[0] == 0xff)
+			lm75a++;
+	}
+	if (lm75a == 3)
+		sc->sc_hwtype = HWTYPE_LM75A;
+
+	/* Restore the configuration register. */
+	buf8[0] = LM75_CONF;
+	buf8[1] = conf;
+	if (lm75_write(sc->sc_dev, sc->sc_addr, buf8, 2) < 0)
+		return (-1);
+
+	return (0);
+}
+
+static void
+lm75_start(void *xdev)
+{
+	device_t dev;
+	struct lm75_softc *sc;
+	struct sysctl_ctx_list *ctx;
+	struct sysctl_oid *tree_node;
+	struct sysctl_oid_list *tree;
+
+	dev = (device_t)xdev;
+	sc = device_get_softc(dev);
+	ctx = device_get_sysctl_ctx(dev);
+	tree_node = device_get_sysctl_tree(dev);
+	tree = SYSCTL_CHILDREN(tree_node);
+
+	config_intrhook_disestablish(&sc->enum_hook);
+
+	/*
+	 * Detect the kind of chip we are attaching to.
+	 * This may not work for LM75 clones.
+	 */
+	if (lm75_type_detect(sc) != 0) {
+		device_printf(dev, "cannot read from sensor.\n");
+		return;
+	}
+	if (sc->sc_hwtype == HWTYPE_LM75A)
+		device_printf(dev,
+		    "LM75A type sensor detected (11bits resolution).\n");
+
+	/* Read the configuration register. */
+	if (lm75_conf_read(sc) != 0) {
+		device_printf(dev, "cannot read the configuration register.\n");
+		return;
+	}
+
+	/* Temperature. */
+	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "temperature",
+	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, LM75_TEMP,
+	    lm75_temp_sysctl, "IK", "Current temperature");
+	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "thyst",
+	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, LM75_THYST,
+	    lm75_temp_sysctl, "IK", "Hysteresis temperature");
+	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "tos",
+	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, LM75_TOS,
+	    lm75_temp_sysctl, "IK", "Overtemperature");
+
+	/* Configuration parameters. */
+	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "faults",
+	    CTLFLAG_RW | CTLTYPE_UINT, dev, 0,
+	    lm75_faults_sysctl, "IU", "LM75 fault queue");
+	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "mode",
+	    CTLFLAG_RW | CTLTYPE_STRING, dev, 0,
+	    lm75_mode_sysctl, "A", "LM75 mode");
+	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "polarity",
+	    CTLFLAG_RW | CTLTYPE_STRING, dev, 0,
+	    lm75_pol_sysctl, "A", "LM75 OS polarity");
+	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "shutdown",
+	    CTLFLAG_RW | CTLTYPE_UINT, dev, 0,
+	    lm75_shutdown_sysctl, "IU", "LM75 shutdown");
+}
+
+static int
+lm75_conf_read(struct lm75_softc *sc)
+{
+	uint8_t buf8;
+
+	if (lm75_read(sc->sc_dev, sc->sc_addr, LM75_CONF, &buf8, 1) < 0)
+		return (-1);
+
+	sc->sc_conf = (uint32_t)buf8;
+
+	return (0);
+}
+
+static int
+lm75_conf_write(struct lm75_softc *sc)
+{
+	uint8_t buf8[2];
+
+	buf8[0] = LM75_CONF;
+	buf8[1] = (uint8_t)sc->sc_conf & LM75_CONF_MASK;
+
+	if (lm75_write(sc->sc_dev, sc->sc_addr, buf8, 2) < 0)
+		return (-1);
+
+	return (0);
+}
+
+static int
+lm75_temp_read(struct lm75_softc *sc, uint8_t reg, int *temp)
+{
+	uint8_t buf8[2];
+	uint16_t buf;
+	int t;
+
+	if (lm75_read(sc->sc_dev, sc->sc_addr, reg, buf8, 2) < 0)
+		return (-1);
+
+	buf = (buf8[0] << 8) | (buf8[1] & 0xff);
+
+	/*
+	 * LM75 has a 9 bit ADC with resolution of 0.5 C per bit.
+	 * LM75A has an 11 bit ADC with resolution of 0.125 C per bit.
+	 * Temperature is stored with two's complement.
+	 */
+	if (buf & LM75_NEG_BIT)
+		buf = ~buf + 1;
+	*temp = ((int16_t)buf >> 8) * 10;
+	t = 0;
+	if (sc->sc_hwtype == HWTYPE_LM75A) {
+		if (buf & LM75_0125C)
+			t += 125;
+		if (buf & LM75_0250C)
+			t += 250;
+	}
+	if (buf & LM75_0500C)
+		t += 500;
+	t /= 100;
+	*temp += t;
+	if (buf & LM75_NEG_BIT)
+		*temp = -(*temp);
+	*temp += TZ_ZEROC;
+
+	return (0);
+}
+
+static int
+lm75_temp_write(struct lm75_softc *sc, uint8_t reg, int temp)
+{
+	uint8_t buf8[3];
+	uint16_t buf;
+
+	if (temp > LM75_MAX_TEMP)
+		temp = LM75_MAX_TEMP;
+	if (temp < LM75_MIN_TEMP)
+		temp = LM75_MIN_TEMP;
+
+	buf = (uint16_t)temp;
+	buf <<= 8;
+
+	buf8[0] = reg;
+	buf8[1] = buf >> 8;
+	buf8[2] = buf & 0xff;
+
+	if (lm75_write(sc->sc_dev, sc->sc_addr, buf8, 3) < 0)
+		return (-1);
+
+	return (0);
+}
+
+static int
+lm75_str_mode(char *buf)
+{
+	int len, rtrn;
+
+	rtrn = -1;
+	len = strlen(buf);
+	if (len > 2 && strncasecmp("interrupt", buf, len) == 0)
+		rtrn = 1;
+	else if (len > 2 && strncasecmp("comparator", buf, len) == 0)
+		rtrn = 0;
+
+	return (rtrn);
+}
+
+static int
+lm75_str_pol(char *buf)
+{
+	int len, rtrn;
+
+	rtrn = -1;
+	len = strlen(buf);
+	if (len > 1 && strncasecmp("high", buf, len) == 0)
+		rtrn = 1;
+	else if (len > 1 && strncasecmp("low", buf, len) == 0)
+		rtrn = 0;
+	else if (len > 8 && strncasecmp("active-high", buf, len) == 0)
+		rtrn = 1;
+	else if (len > 8 && strncasecmp("active-low", buf, len) == 0)
+		rtrn = 0;
+
+	return (rtrn);
+}
+
+static int
+lm75_temp_sysctl(SYSCTL_HANDLER_ARGS)
+{
+	device_t dev;
+	int error, temp;
+	struct lm75_softc *sc;
+	uint8_t reg;
+
+	dev = (device_t)arg1;
+	reg = (uint8_t)arg2;
+	sc = device_get_softc(dev);
+
+	if (lm75_temp_read(sc, reg, &temp) != 0)
+		return (EIO);
+
+	error = sysctl_handle_int(oidp, &temp, 0, req);
+	if (error != 0 || req->newptr == NULL)
+		return (error);
+
+	if (lm75_temp_write(sc, reg, temp) != 0)
+		return (EIO);
+
+	return (error);
+}
+
+static int
+lm75_faults_sysctl(SYSCTL_HANDLER_ARGS)
+{
+	device_t dev;
+	int error, faults, i, newf, tmp;
+	struct lm75_softc *sc;
+
+	dev = (device_t)arg1;
+	sc = device_get_softc(dev);
+	tmp = (sc->sc_conf & LM75_CONF_FAULT) >> LM75_CONF_FSHIFT;
+	if (tmp > nitems(lm75_faults))
+		tmp = nitems(lm75_faults);
+	faults = lm75_faults[tmp];
+
+	error = sysctl_handle_int(oidp, &faults, 0, req);
+	if (error != 0 || req->newptr == NULL)
+		return (error);
+
+	if (faults != lm75_faults[tmp]) {
+		newf = 0;
+		for (i = 0; i < nitems(lm75_faults); i++)
+			if (faults >= lm75_faults[i])
+				newf = i;
+		sc->sc_conf &= ~LM75_CONF_FAULT;
+		sc->sc_conf |= newf << LM75_CONF_FSHIFT;
+		if (lm75_conf_write(sc) != 0)
+			return (EIO);
+	}
+
+	return (error);
+}
+
+static int
+lm75_mode_sysctl(SYSCTL_HANDLER_ARGS)
+{
+	char buf[16];
+	device_t dev;
+	int error, mode, newm;
+	struct lm75_softc *sc;
+
+	dev = (device_t)arg1;
+	sc = device_get_softc(dev);
+	if (sc->sc_conf & LM75_CONF_MODE) {
+		mode = 1;
+		strlcpy(buf, "interrupt", sizeof(buf));
+	} else {
+		mode = 0;
+		strlcpy(buf, "comparator", sizeof(buf));
+	}
+
+	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
+	if (error != 0 || req->newptr == NULL)
+		return (error);
+
+	newm = lm75_str_mode(buf);
+	if (newm != -1 && mode != newm) {
+		sc->sc_conf &= ~LM75_CONF_MODE;
+		if (newm == 1)
+			sc->sc_conf |= LM75_CONF_MODE;
+		if (lm75_conf_write(sc) != 0)
+			return (EIO);
+	}
+
+	return (error);
+}
+
+static int
+lm75_pol_sysctl(SYSCTL_HANDLER_ARGS)
+{
+	char buf[16];
+	device_t dev;
+	int error, newp, pol;
+	struct lm75_softc *sc;
+
+	dev = (device_t)arg1;
+	sc = device_get_softc(dev);
+	if (sc->sc_conf & LM75_CONF_POL) {
+		pol = 1;
+		strlcpy(buf, "active-high", sizeof(buf));
+	} else {
+		pol = 0;
+		strlcpy(buf, "active-low", sizeof(buf));
+	}
+
+	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
+	if (error != 0 || req->newptr == NULL)
+		return (error);
+
+	newp = lm75_str_pol(buf);
+	if (newp != -1 && pol != newp) {
+		sc->sc_conf &= ~LM75_CONF_POL;
+		if (newp == 1)
+			sc->sc_conf |= LM75_CONF_POL;
+		if (lm75_conf_write(sc) != 0)
+			return (EIO);
+	}
+
+	return (error);
+}
+
+static int
+lm75_shutdown_sysctl(SYSCTL_HANDLER_ARGS)
+{
+	device_t dev;
+	int error, shutdown, tmp;
+	struct lm75_softc *sc;
+
+	dev = (device_t)arg1;
+	sc = device_get_softc(dev);
+	tmp = shutdown = (sc->sc_conf & LM75_CONF_SHUTD) ? 1 : 0;
+
+	error = sysctl_handle_int(oidp, &shutdown, 0, req);
+	if (error != 0 || req->newptr == NULL)
+		return (error);
+
+	if (shutdown != tmp) {
+		sc->sc_conf &= ~LM75_CONF_SHUTD;
+		if (shutdown)
+			sc->sc_conf |= LM75_CONF_SHUTD;
+		if (lm75_conf_write(sc) != 0)
+			return (EIO);
+	}
+
+	return (error);
+}
Index: share/man/man4/Makefile
===================================================================
--- share/man/man4/Makefile	(revision 263740)
+++ share/man/man4/Makefile	(working copy)
@@ -228,6 +228,7 @@
 	lge.4 \
 	${_lindev.4} \
 	${_linux.4} \
+	lm75.4 \
 	lmc.4 \
 	lo.4 \
 	lp.4 \
--- /dev/null	2014-03-27 11:11:00.000000000 -0300
+++ share/man/man4/lm75.4	2014-03-27 11:10:13.562117611 -0300
@@ -0,0 +1,189 @@
+.\"
+.\" Copyright (c) 2014 Luiz Otavio O Souza <loos at freebsd.org>
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd March 27, 2014
+.Dt LM75 4
+.Os
+.Sh NAME
+.Nm lm75
+.Nd lm75 i2c digital temperature sensor driver
+.Sh SYNOPSIS
+.Cd "device iic"
+.Cd "device iicbus"
+.Cd "device lm75"
+.Sh DESCRIPTION
+The
+.Nm
+driver provides access to sensor data and configuration over the
+.Xr iicbus 4 .
+.Pp
+It provides an easy and simple way to check the functionality of an i2c bus
+as it provides read and write access to the
+.Nm
+configuration register.
+.Pp
+The access to
+.Nm
+data is made via the
+.Xr sysctl 8
+interface:
+.Bd -literal
+dev.lm75.0.%desc: LM75 temperature sensor
+dev.lm75.0.%driver: lm75
+dev.lm75.0.%location: addr=0x49
+dev.lm75.0.%pnpinfo: name=lm750 compat=national,lm75
+dev.lm75.0.%parent: iicbus3
+dev.lm75.0.temperature: 27.1C
+dev.lm75.0.thyst: 75.0C
+dev.lm75.0.tos: 80.0C
+dev.lm75.0.faults: 1
+dev.lm75.0.mode: comparator
+dev.lm75.0.polarity: active-low
+dev.lm75.0.shutdown: 0
+.Ed
+.Bl -tag -width ".Va dev.lm75.%d.temperature"
+.It Va dev.lm75.%d.temperature
+Is the read-only value of the current temperature read by the sensor.
+.It Va dev.lm75.%d.thyst
+Sets the hysteresis temperature.
+Once the temperature get over the overtemperature shutdown value (tos)
+it need to drop bellow the hysteresis temperature to disable the output
+(interrupt) pin again.
+.It Va dev.lm75.%d.tos
+Sets the overtemperature shutdown value.
+Once the temperature get over this value the output pin will be enabled.
+The way the output (interrupt) pin works, depends on the mode configuration.
+.It Va dev.lm75.%d.faults
+Is the number of faults that must occur consecutively to activate the
+interrupt (output) pin.
+It can be set to 1, 2, 4, and 6.
+.It Va dev.lm75.%d.mode
+Set the operation mode for the sensor interrupt pin.
+It can be set to 'comparator' (default) or 'interrupt'.
+.It Va dev.lm75.%d.polarity
+Set the polarity of the sensor interrupt pin.
+It can be set to 'active-low' (default) or 'active-high'.
+Please note that the output pin is an open-drain output and it needs a
+proper pull-up resistor to work.
+.It Va dev.lm75.%d.shutdown
+When set to '1' it shutdown the sensor.
+The temperature convertion stops but the sensor remains with its i2c bus
+active, i.e., it can be woken up by setting this option to '0' again.
+.El
+.Pp
+Please check the
+.Nm
+datasheet for more details.
+.Pp
+When used together with
+.Xr snmp_lm75 3
+it allows the monitoring of
+.Nm
+temperature data over SNMP.
+.Pp
+The
+.Nm
+driver supports both the low and the high resolution models.
+.Pp
+The low resolution model (lm75) provides a 9 bit output with the LSB
+representing 0.5C.
+.Pp
+The high resolution model (lm75a) provides an 11 bit output with the LSB
+representing 0.125C.
+.Pp
+The driver tries to auto-detect the
+.Nm
+model, but the detection of some
+.Nm
+clones may not work reliably.
+.Pp
+On a
+.Xr device.hints 5
+based system, like
+.Li MIPS ,
+these values are configurable for the
+.Nm :
+.Bl -tag -width ".Va hint.lm75.%d.addr"
+.It Va hint.lm75.%d.at
+Is the
+.Xr iicbus 4
+you are attaching to.
+.It Va hint.lm75.%d.addr
+Is the
+.Nm
+i2c address on the
+.Xr iicbus 4 .
+.El
+.Pp
+On a
+.Xr FDT 4
+based system, like
+.Li ARM ,
+the DTS part for a
+.Nm
+device usually looks like:
+.Bd -literal
+i2c {
+
+	...
+
+	lm750 {
+		compatible = "national,lm75";
+		i2c-address = <0x49>;
+	};
+};
+.Ed
+.Pp
+Where:
+.Bl -tag -width ".Va i2c-address"
+.It Va compatible
+Should always be set to "national,lm75".
+.It Va i2c-address
+The
+.Va i2c-address
+property indicates which i2c address the
+.Nm
+is wired at.
+.Nm
+temperature sensors can be wired to 8 different address, allowing up to 8
+sensors on the same
+.Xr iicbus 4 .
+.El
+.Sh SEE ALSO
+.Xr snmp_lm75 3 ,
+.Xr fdt 4 ,
+.Xr iic 4 ,
+.Xr iicbus 4 ,
+.Xr sysctl 8
+.Sh HISTORY
+The
+.Nm
+driver first appeared in
+.Fx 11.0 .
+.Sh AUTHORS
+.An -nosplit
+The driver and this manual page was written by
+.An Luiz Otavio O Souza Aq loos at FreeBSD.org
-------------- next part --------------
Index: etc/snmpd.config
===================================================================
--- etc/snmpd.config	(revision 262860)
+++ etc/snmpd.config	(working copy)
@@ -296,6 +296,12 @@
 #begemotSnmpdModulePath."bridge" = "/usr/lib/snmp_bridge.so"
 
 #
+# LM75 Sensor module
+#  This requires the mibII module.
+#
+#begemotSnmpdModulePath."lm75" = "/usr/lib/snmp_lm75.so"
+
+#
 # Wireless module
 #  This requires the mibII module.
 #
Index: usr.sbin/bsnmpd/modules/Makefile
===================================================================
--- usr.sbin/bsnmpd/modules/Makefile	(revision 262860)
+++ usr.sbin/bsnmpd/modules/Makefile	(working copy)
@@ -12,6 +12,7 @@
 	snmp_bridge \
 	snmp_hast \
 	snmp_hostres \
+	snmp_lm75 \
 	snmp_mibII \
 	snmp_target \
 	snmp_usm \
Index: usr.sbin/bsnmpd/modules/snmp_lm75/BEGEMOT-LM75-MIB.txt
===================================================================
--- usr.sbin/bsnmpd/modules/snmp_lm75/BEGEMOT-LM75-MIB.txt	(revision 0)
+++ usr.sbin/bsnmpd/modules/snmp_lm75/BEGEMOT-LM75-MIB.txt	(working copy)
@@ -0,0 +1,160 @@
+--
+-- Copyright (c) 2014 Luiz Otavio O Souza <loos at FreeBSD.org>
+-- All rights reserved.
+--
+-- Redistribution and use in source and binary forms, with or without
+-- modification, are permitted provided that the following conditions
+-- are met:
+-- 1. Redistributions of source code must retain the above copyright
+--    notice, this list of conditions and the following disclaimer.
+-- 2. Redistributions in binary form must reproduce the above copyright
+--    notice, this list of conditions and the following disclaimer in the
+--    documentation and/or other materials provided with the distribution.
+--
+-- THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+-- ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+-- SUCH DAMAGE.
+--
+-- $FreeBSD$
+--
+
+BEGEMOT-LM75-MIB DEFINITIONS ::= BEGIN
+
+IMPORTS
+    MODULE-IDENTITY, OBJECT-TYPE, NOTIFICATION-TYPE,
+    Counter64, Integer32
+	FROM SNMPv2-SMI
+    TEXTUAL-CONVENTION, RowStatus
+	FROM SNMPv2-TC
+    begemot
+	FROM BEGEMOT-MIB;
+
+begemotLoos MODULE-IDENTITY
+    LAST-UPDATED "201402240000Z"
+    ORGANIZATION "FreeBSD"
+    CONTACT-INFO
+	    "		Luiz Otavio O Souza
+
+	     Postal:	N/A
+
+	     Fax:	N/A
+
+	     E-Mail:	loos at FreeBSD.org"
+    DESCRIPTION
+	    "The Begemot MIB for reading lm75 sensors data."
+    REVISION     "201402240000Z"
+    DESCRIPTION
+	    "Initial revision."
+    ::= { begemot 400 }
+
+begemotLm75Objects	OBJECT IDENTIFIER ::= { begemotLm75 1 }
+
+-- ---------------------------------------------------------- --
+-- Configuration parameters
+-- ---------------------------------------------------------- --
+
+lm75Sensor	OBJECT IDENTIFIER ::= { begemotlm75Objects 1 }
+
+lm75Sensors	OBJECT-TYPE
+    SYNTAX	Integer32
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"Number of LM75 sensors in the system."
+    ::= { lm75Sensors 1 }
+
+-- ---------------------------------------------------------- --
+-- TempSensor Table
+-- ---------------------------------------------------------- --
+lm75SensorTable OBJECT-TYPE
+    SYNTAX	SEQUENCE OF Lm75SensorEntry
+    MAX-ACCESS	not-accessible
+    STATUS	current
+    DESCRIPTION
+	"A table containing information about all temperature sensors."
+    ::= { begemotLm75Objects 2 }
+
+loosTempSensorEntry OBJECT-TYPE
+    SYNTAX	Lm75SensorEntry
+    MAX-ACCESS	not-accessible
+    STATUS	current
+    DESCRIPTION
+	"Table entry that describes one temperature sensor."
+    INDEX	{ lm75SensorIndex }
+    ::= { lm75SensorTable 1 }
+
+Lm75SensorEntry ::= SEQUENCE {
+    lm75SensorIndex			Integer32,
+    lm75SensorSysctlIndex		Integer32,
+    lm75SensorDesc			OCTET STRING,
+    lm75SensorLocation			OCTET STRING,
+    lm75SensorPnpInfo			OCTET STRING,
+    lm75SensorParent			OCTET STRING,
+    lm75SensorTemperature		Integer32
+}
+
+lm75SensorIndex OBJECT-TYPE
+    SYNTAX	Integer32
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"LM75 Sensor index."
+    ::= { lm75SensorEntry 1 }
+
+lm75SensorSysctlIndex OBJECT-TYPE
+    SYNTAX	Integer32
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"LM75 Sensor sysctl index."
+    ::= { lm75SensorEntry 2 }
+
+lm75SensorDesc OBJECT-TYPE
+    SYNTAX	OCTET STRING
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"LM75 Sensor description."
+    ::= { lm75SensorEntry 3 }
+
+lm75SensorLocation OBJECT-TYPE
+    SYNTAX	OCTET STRING
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"LM75 Sensor location."
+    ::= { lm75SensorEntry 4 }
+
+lm75SensorPnpInfo OBJECT-TYPE
+    SYNTAX	OCTET STRING
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"LM75 Sensor pnp information."
+    ::= { lm75SensorEntry 5 }
+
+lm75SensorParent OBJECT-TYPE
+    SYNTAX	OCTET STRING
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"LM75 Sensor parent bus."
+    ::= { lm75SensorEntry 6 }
+
+lm75SensorTemperature OBJECT-TYPE
+    SYNTAX	Integer32
+    MAX-ACCESS	read-only
+    STATUS	current
+    DESCRIPTION
+	"LM75 Sensor temperature."
+    ::= { lm75SensorEntry 7 }
+
+END

Property changes on: usr.sbin/bsnmpd/modules/snmp_lm75/BEGEMOT-LM75-MIB.txt
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+FreeBSD=%H
\ No newline at end of property
Index: usr.sbin/bsnmpd/modules/snmp_lm75/Makefile
===================================================================
--- usr.sbin/bsnmpd/modules/snmp_lm75/Makefile	(revision 0)
+++ usr.sbin/bsnmpd/modules/snmp_lm75/Makefile	(working copy)
@@ -0,0 +1,13 @@
+# $FreeBSD$
+
+.include <bsd.own.mk>
+
+MOD=    lm75
+SRCS=   snmp_lm75.c
+XSYM=   begemotLm75
+MAN=    snmp_lm75.3
+
+BMIBS=  BEGEMOT-LM75-MIB.txt
+DEFS=   ${MOD}_tree.def
+
+.include <bsd.snmpmod.mk>

Property changes on: usr.sbin/bsnmpd/modules/snmp_lm75/Makefile
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+FreeBSD=%H
\ No newline at end of property
Index: usr.sbin/bsnmpd/modules/snmp_lm75/lm75_tree.def
===================================================================
--- usr.sbin/bsnmpd/modules/snmp_lm75/lm75_tree.def	(revision 0)
+++ usr.sbin/bsnmpd/modules/snmp_lm75/lm75_tree.def	(working copy)
@@ -0,0 +1,56 @@
+#-
+# Copyright (c) 2014 Luiz Otavio O Souza <loos at FreeBSD.org>
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# $FreeBSD$
+#
+
+(1 internet
+  (4 private
+    (1 enterprises
+      (12325 fokus
+        (1 begemot
+          (400 begemotLm75
+            (1 begemotLm75Objects
+              (1 lm75Sensors
+                (1 lm75Sensors INTEGER32 op_lm75Sensors GET)
+              )
+              (2 lm75SensorTable
+                (1 lm75SensorEntry : OCTETSTRING op_lm75SensorTable
+                  (1 lm75SensorIndex INTEGER32 GET)
+                  (2 lm75SensorSysctlIndex INTEGER32 GET)
+                  (3 lm75SensorDesc OCTETSTRING GET)
+                  (4 lm75SensorLocation OCTETSTRING GET)
+                  (5 lm75SensorPnpInfo OCTETSTRING GET)
+                  (6 lm75SensorParent OCTETSTRING GET)
+                  (7 lm75SensorTemperature INTEGER32 GET)
+                )
+              )
+            )
+          )
+        )
+      )
+    )
+  )
+)
Index: usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.3
===================================================================
--- usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.3	(revision 0)
+++ usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.3	(working copy)
@@ -0,0 +1,60 @@
+.\"-
+.\" Copyright (c) 2014 Luiz Otavio O Souza <loos at FreeBSD.org>
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd February 24, 2014
+.Dt SNMP_LM75 3
+.Os
+.Sh NAME
+.Nm snmp_lm75
+.Nd "LM75 Sensor module for"
+.Xr bsnmpd 1
+.Sh LIBRARY
+.Pq begemotSnmpdModulePath."lm75" = "/usr/lib/snmp_lm75.so"
+.Sh DESCRIPTION
+The
+.Nm snmp_lm75
+module implements a private BEGEMOT-LM75-MIB, which allows
+reading the temperature of the LM75 sensors on the system.
+.Pp
+The module reads the sensor(s) temperature using the
+.Xr sysctl 8
+API.
+.Sh FILES
+.Bl -tag -width "XXXXXXXXX"
+.It Pa /usr/share/snmp/defs/lm75_tree.def
+The description of the MIB tree implemented by
+.Nm .
+.It Pa /usr/share/snmp/mibs/BEGEMOT-LM75-MIB.txt
+The private BEGEMOT-LM75-MIB that is implemented by this module.
+.El
+.Sh SEE ALSO
+.Xr bsnmpd 1 ,
+.Xr gensnmptree 1 ,
+.Xr snmpmod 3 ,
+.Xr lm75 4
+.Sh AUTHORS
+.An Luiz Otavio O Souza Aq loos at FreeBSD.org

Property changes on: usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.3
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+FreeBSD=%H
\ No newline at end of property
Index: usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.c
===================================================================
--- usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.c	(revision 0)
+++ usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.c	(working copy)
@@ -0,0 +1,436 @@
+/*-
+ * Copyright (c) 2014 Luiz Otavio O Souza <loos at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/queue.h>
+#include <sys/sysctl.h>
+
+#include <bsnmp/snmpmod.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+
+#include "lm75_oid.h"
+#include "lm75_tree.h"
+
+#ifndef	LM75BUF
+#define	LM75BUF		64
+#endif
+#define	TZ_ZEROC	2732
+#define UPDATE_INTERVAL	500	/* update interval in ticks */
+
+static struct lmodule *module;
+
+static const struct asn_oid oid_lm75 = OIDX_begemotLm75;
+
+/* the Object Resource registration index */
+static u_int lm75_index = 0;
+
+/* Number of available sensors in the system. */
+static int lm75_sensors;
+
+/*
+ * Structure that describes single sensor.
+ */
+struct lm75_snmp_sensor {
+	TAILQ_ENTRY(lm75_snmp_sensor) link;
+	int32_t		index;
+	int32_t		sysctlidx;
+	int32_t		temp;
+	char		desc[LM75BUF];
+	char		location[LM75BUF];
+	char		parent[LM75BUF];
+	char		pnpinfo[LM75BUF];
+};
+
+static TAILQ_HEAD(, lm75_snmp_sensor) sensors =
+    TAILQ_HEAD_INITIALIZER(sensors);
+
+/* Ticks of the last sensors reading. */
+static uint64_t last_sensors_update;
+
+static void free_sensors(void);
+static int lm75_fini(void);
+static int lm75_init(struct lmodule *mod, int argc, char *argv[]);
+static void lm75_start(void);
+static int update_sensors(void);
+
+const struct snmp_module config = {
+    .comment   =
+	"This module implements the BEGEMOT MIB for reading LM75 sensors data.",
+    .init      = lm75_init,
+    .start     = lm75_start,
+    .fini      = lm75_fini,
+    .tree      = lm75_ctree,
+    .tree_size = lm75_CTREE_SIZE,
+};
+
+static int
+lm75_init(struct lmodule *mod, int argc __unused, char *argv[] __unused)
+{
+
+	module = mod;
+
+	lm75_sensors = 0;
+	openlog("snmp_lm75", LOG_NDELAY | LOG_PID, LOG_DAEMON);
+
+	return(0);
+}
+
+static void
+lm75_start(void)
+{
+
+	lm75_index = or_register(&oid_lm75,
+	    "The MIB module for reading lm75 sensors data.", module);
+}
+
+static int
+lm75_fini(void)
+{
+
+	or_unregister(lm75_index);
+	free_sensors();
+	closelog();
+
+	return (0);
+}
+
+static void
+free_sensors(void)
+{
+	struct lm75_snmp_sensor *sensor;
+
+	while ((sensor = TAILQ_FIRST(&sensors)) != NULL) {
+		TAILQ_REMOVE(&sensors, sensor, link);
+		free(sensor);
+	}
+}
+
+static int
+sysctlname(int *oid, int nlen, char *name, size_t len)
+{
+	int mib[12];
+
+	if (nlen > (int)sizeof(mib) + 2)
+		return (-1);
+
+	mib[0] = 0;
+	mib[1] = 1;
+	memcpy(mib + 2, oid, nlen * sizeof(int));
+
+	if (sysctl(mib, nlen + 2, name, &len, 0, 0) == -1)
+		return (-1);
+
+	return (0);
+}
+
+static int
+sysctlgetnext(int *oid, int nlen, int *next, size_t *nextlen)
+{
+	int mib[12];
+
+	if (nlen  > (int)sizeof(mib) + 2)
+		return (-1);
+
+	mib[0] = 0;
+	mib[1] = 2;
+	memcpy(mib + 2, oid, nlen * sizeof(int));
+
+	if (sysctl(mib, nlen + 2, next, nextlen, 0, 0) == -1)
+		return (-1);
+
+	return (0);
+}
+
+static int
+update_sensor_sysctl(char *obuf, size_t *obuflen, int idx, const char *name)
+{
+	char buf[LM75BUF];
+	int mib[5];
+	size_t len;
+
+	/* Fill out the mib information. */
+	snprintf(buf, sizeof(buf) - 1, "dev.lm75.%d.%s", idx, name);
+	len = 4;
+	if (sysctlnametomib(buf, mib, &len) == -1)
+		return (-1);
+
+	/* Read the sysctl data. */
+	if (sysctl(mib, len, obuf, obuflen, NULL, 0) == -1)
+		return (-1);
+
+	return (0);
+}
+
+static void
+update_sensor(struct lm75_snmp_sensor *sensor, int idx)
+{
+	size_t len;
+
+	len = sizeof(sensor->desc);
+	update_sensor_sysctl(sensor->desc, &len, idx, "%desc");
+
+	len = sizeof(sensor->location);
+	update_sensor_sysctl(sensor->location, &len, idx, "%location");
+
+	len = sizeof(sensor->pnpinfo);
+	update_sensor_sysctl(sensor->pnpinfo, &len, idx, "%pnpinfo");
+
+	len = sizeof(sensor->parent);
+	update_sensor_sysctl(sensor->parent, &len, idx, "%parent");
+}
+
+static int
+add_sensor(char *buf, size_t nlen)
+{
+	int idx, mib[5], temp;
+	size_t len;
+	struct lm75_snmp_sensor *sensor;
+
+	if (sscanf(buf, "dev.lm75.%d.temperature", &idx) != 1)
+		return (-1);
+
+	/* Fill out the mib information. */
+	if (sysctlnametomib(buf, mib, &nlen) == -1)
+		return (-1);
+
+	/* Read the sensor temperature. */
+	len = sizeof(temp);
+	if (sysctl(mib, nlen, &temp, &len, NULL, 0) == -1)
+		return (-1);
+
+	/* Add the sensor data to the table. */
+	sensor = calloc(1, sizeof(*sensor));
+	if (sensor == NULL) {
+		syslog(LOG_ERR, "Unable to allocate %zu bytes for resource",
+		    sizeof(*sensor));
+		return (-1);
+	}
+	sensor->index = ++lm75_sensors;
+	sensor->sysctlidx = idx;
+	sensor->temp = (temp - TZ_ZEROC) / 10;
+	TAILQ_INSERT_TAIL(&sensors, sensor, link);
+
+	update_sensor(sensor, idx);
+
+	return (0);
+}
+
+static int
+update_sensors(void)
+{
+	char buf[LM75BUF];
+	int i, root[5], *next, *oid;
+	size_t len, nextlen, rootlen;
+	static uint64_t now;
+
+	now = get_ticks();
+	if (now - last_sensors_update < UPDATE_INTERVAL)
+		return (0);
+
+	last_sensors_update = now;
+
+	/* Reset the sensor data. */
+	free_sensors();
+	lm75_sensors = 0;
+
+	/* Start from the lm75 default root node. */
+	rootlen = 2;
+	if (sysctlnametomib("dev.lm75", root, &rootlen) == -1)
+		return (0);
+
+	oid = (int *)malloc(sizeof(int) * rootlen);
+	if (oid == NULL) {
+		perror("malloc");
+		return (-1);
+	}
+	memcpy(oid, root, rootlen * sizeof(int));
+	len = rootlen;
+
+	/* Traverse the sysctl(3) interface and find the active sensors. */
+	for (;;) {
+
+		/* Find the size of the next mib. */
+		nextlen = 0;
+		if (sysctlgetnext(oid, len, NULL, &nextlen) == -1) {
+			free(oid);
+			return (0);
+		}
+		/* Alocate and read the next mib. */
+		next = (int *)malloc(nextlen);
+		if (next == NULL) {
+			syslog(LOG_ERR,
+			    "Unable to allocate %zu bytes for resource",
+			    nextlen);
+			free(oid);
+			return (-1);
+		}
+		if (sysctlgetnext(oid, len, next, &nextlen) == -1) {
+			free(oid);
+			free(next);
+			return (0);
+		}
+		free(oid);
+		/* Check if we care about the next mib. */
+		for (i = 0; i < (int)rootlen; i++)
+			if (next[i] != root[i]) {
+				free(next);
+				return (0);
+			}
+		oid = (int *)malloc(nextlen);
+		if (oid == NULL) {
+			syslog(LOG_ERR,
+			    "Unable to allocate %zu bytes for resource",
+			    nextlen);
+			free(next);
+			return (-1);
+		}
+		memcpy(oid, next, nextlen);
+		free(next);
+		len = nextlen / sizeof(int);
+
+		/* Find the mib name. */
+		if (sysctlname(oid, len, buf, sizeof(buf)) != 0)
+			continue;
+
+		if (strstr(buf, "temperature"))
+			if (add_sensor(buf, len) != 0) {
+				free(oid);
+				return (-1);
+			}
+	}
+
+	return (0);
+}
+
+int
+op_lm75Sensors(struct snmp_context *context __unused, struct snmp_value *value,
+    u_int sub, u_int iidx __unused, enum snmp_op op)
+{
+	asn_subid_t which;
+
+	if (update_sensors() == -1)
+		return (SNMP_ERR_RES_UNAVAIL);
+
+	which = value->var.subs[sub - 1];
+
+	switch (op) {
+	case SNMP_OP_GET:
+		switch (which) {
+		case LEAF_lm75Sensors:
+			value->v.integer = lm75_sensors;
+			break;
+		default:
+			return (SNMP_ERR_RES_UNAVAIL);
+		}
+		break;
+	case SNMP_OP_SET:
+		return (SNMP_ERR_NOT_WRITEABLE);
+	case SNMP_OP_GETNEXT:
+	case SNMP_OP_ROLLBACK:
+	case SNMP_OP_COMMIT:
+		return (SNMP_ERR_NOERROR);
+	default:
+		return (SNMP_ERR_RES_UNAVAIL);
+	}
+
+	return (SNMP_ERR_NOERROR);
+}
+
+int
+op_lm75SensorTable(struct snmp_context *context __unused,
+    struct snmp_value *value, u_int sub, u_int iidx __unused, enum snmp_op op)
+{
+	struct lm75_snmp_sensor *sensor;
+	asn_subid_t which;
+	int ret;
+
+	if (update_sensors() == -1)
+		return (SNMP_ERR_RES_UNAVAIL);
+
+	which = value->var.subs[sub - 1];
+
+	switch (op) {
+	case SNMP_OP_GETNEXT:
+		sensor = NEXT_OBJECT_INT(&sensors, &value->var, sub);
+		if (sensor == NULL)
+			return (SNMP_ERR_NOSUCHNAME);
+		value->var.len = sub + 1;
+		value->var.subs[sub] = sensor->index;
+		break;
+	case SNMP_OP_GET:
+		if (value->var.len - sub != 1)
+			return (SNMP_ERR_NOSUCHNAME);
+		sensor = FIND_OBJECT_INT(&sensors, &value->var, sub);
+		if (sensor == NULL)
+			return (SNMP_ERR_NOSUCHNAME);
+		break;
+	case SNMP_OP_SET:
+		return (SNMP_ERR_NOT_WRITEABLE);
+	case SNMP_OP_ROLLBACK:
+	case SNMP_OP_COMMIT:
+		return (SNMP_ERR_NOERROR);
+	default:
+		return (SNMP_ERR_RES_UNAVAIL);
+	}
+
+	ret = SNMP_ERR_NOERROR;
+
+	switch (which) {
+	case LEAF_lm75SensorIndex:
+		value->v.integer = sensor->index;
+		break;
+	case LEAF_lm75SensorSysctlIndex:
+		value->v.integer = sensor->sysctlidx;
+		break;
+	case LEAF_lm75SensorDesc:
+		ret = string_get(value, sensor->desc, -1);
+		break;
+	case LEAF_lm75SensorLocation:
+		ret = string_get(value, sensor->location, -1);
+		break;
+	case LEAF_lm75SensorPnpInfo:
+		ret = string_get(value, sensor->pnpinfo, -1);
+		break;
+	case LEAF_lm75SensorParent:
+		ret = string_get(value, sensor->parent, -1);
+		break;
+	case LEAF_lm75SensorTemperature:
+		value->v.integer = sensor->temp;
+		break;
+	default:
+		ret = SNMP_ERR_RES_UNAVAIL;
+		break;
+	}
+
+	return (ret);
+}

Property changes on: usr.sbin/bsnmpd/modules/snmp_lm75/snmp_lm75.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+FreeBSD=%H
\ No newline at end of property


More information about the freebsd-hackers mailing list