Home | History | Annotate | Line # | Download | only in usb
uthum.c revision 1.1.2.1
      1      1.1     tonio /*	$NetBSD: uthum.c,v 1.1.2.1 2010/04/30 14:43:54 uebayasi Exp $   */
      2      1.1     tonio /*	$OpenBSD: uthum.c,v 1.6 2010/01/03 18:43:02 deraadt Exp $   */
      3      1.1     tonio 
      4      1.1     tonio /*
      5      1.1     tonio  * Copyright (c) 2009 Yojiro UO <yuo (at) nui.org>
      6      1.1     tonio  *
      7      1.1     tonio  * Permission to use, copy, modify, and distribute this software for any
      8      1.1     tonio  * purpose with or without fee is hereby granted, provided that the above
      9      1.1     tonio  * copyright notice and this permission notice appear in all copies.
     10      1.1     tonio  *
     11      1.1     tonio  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCAIMS ALL WARRANTIES
     12      1.1     tonio  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13      1.1     tonio  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14      1.1     tonio  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15      1.1     tonio  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16      1.1     tonio  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17      1.1     tonio  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18      1.1     tonio  */
     19      1.1     tonio 
     20      1.1     tonio /* Driver for TEMPer and TEMPerHUM HID */
     21      1.1     tonio 
     22      1.1     tonio #include <sys/param.h>
     23      1.1     tonio #include <sys/proc.h>
     24      1.1     tonio #include <sys/systm.h>
     25      1.1     tonio #include <sys/kernel.h>
     26      1.1     tonio #include <sys/malloc.h>
     27      1.1     tonio #include <sys/device.h>
     28      1.1     tonio #include <sys/conf.h>
     29      1.1     tonio 
     30      1.1     tonio #include <dev/sysmon/sysmonvar.h>
     31      1.1     tonio 
     32      1.1     tonio #include <dev/usb/usb.h>
     33      1.1     tonio #include <dev/usb/usbhid.h>
     34      1.1     tonio #include <dev/usb/usbdi.h>
     35      1.1     tonio #include <dev/usb/usbdi_util.h>
     36      1.1     tonio #include <dev/usb/usbdevs.h>
     37      1.1     tonio #include <dev/usb/uhidev.h>
     38      1.1     tonio #include <dev/usb/hid.h>
     39      1.1     tonio 
     40      1.1     tonio #ifdef USB_DEBUG
     41      1.1     tonio #define UTHUM_DEBUG
     42      1.1     tonio #endif
     43      1.1     tonio 
     44      1.1     tonio #ifdef UTHUM_DEBUG
     45      1.1     tonio int	uthumdebug = 0;
     46      1.1     tonio #define DPRINTFN(n, x)	do { if (uthumdebug > (n)) printf x; } while (0)
     47      1.1     tonio #else
     48      1.1     tonio #define DPRINTFN(n, x)
     49      1.1     tonio #endif
     50      1.1     tonio 
     51      1.1     tonio #define DPRINTF(x) DPRINTFN(0, x)
     52      1.1     tonio 
     53      1.1     tonio 
     54      1.1     tonio /* TEMPerHUM */
     55      1.1     tonio #define CMD_DEVTYPE 0x52 /* XXX */
     56      1.1     tonio #define CMD_GETDATA 0x48 /* XXX */
     57      1.1     tonio #define CMD_GETTEMP 0x54 /* XXX */
     58      1.1     tonio static uint8_t cmd_start[8] =
     59      1.1     tonio 	{ 0x0a, 0x0b, 0x0c, 0x0d, 0x00, 0x00, 0x02, 0x00 };
     60      1.1     tonio static uint8_t cmd_end[8] =
     61      1.1     tonio 	{ 0x0a, 0x0b, 0x0c, 0x0d, 0x00, 0x00, 0x01, 0x00 };
     62      1.1     tonio 
     63      1.1     tonio /* sensors */
     64      1.1     tonio #define UTHUM_TEMP		0
     65      1.1     tonio #define UTHUM_HUMIDITY		1
     66      1.1     tonio #define UTHUM_MAX_SENSORS	2
     67      1.1     tonio 
     68      1.1     tonio #define UTHUM_TYPE_UNKNOWN	0
     69      1.1     tonio #define UTHUM_TYPE_SHT1x	1
     70      1.1     tonio #define UTHUM_TYPE_TEMPER	2
     71      1.1     tonio 
     72      1.1     tonio struct uthum_softc {
     73      1.1     tonio 	struct uhidev		 sc_hdev;
     74      1.1     tonio 	usbd_device_handle	 sc_udev;
     75      1.1     tonio 	u_char			 sc_dying;
     76      1.1     tonio 	uint16_t		 sc_flag;
     77      1.1     tonio 	int			 sc_sensortype;
     78      1.1     tonio 
     79      1.1     tonio 	/* uhidev parameters */
     80      1.1     tonio 	size_t			 sc_flen;	/* feature report length */
     81      1.1     tonio 	size_t			 sc_ilen;	/* input report length */
     82      1.1     tonio 	size_t			 sc_olen;	/* output report length */
     83      1.1     tonio 
     84      1.1     tonio 	/* sensor framework */
     85      1.1     tonio 	struct sysmon_envsys		 *sc_sme;
     86      1.1     tonio 	envsys_data_t			 sc_sensor[UTHUM_MAX_SENSORS];
     87      1.1     tonio 
     88      1.1     tonio 	uint8_t			 sc_num_sensors;
     89      1.1     tonio };
     90      1.1     tonio 
     91      1.1     tonio 
     92      1.1     tonio const struct usb_devno uthum_devs[] = {
     93      1.1     tonio 	/* XXX: various TEMPer variants using same VID/PID */
     94      1.1     tonio 	{ USB_VENDOR_TENX, USB_PRODUCT_TENX_TEMPER},
     95      1.1     tonio };
     96      1.1     tonio #define uthum_lookup(v, p) usb_lookup(uthum_devs, v, p)
     97      1.1     tonio 
     98      1.1     tonio int uthum_match(device_t, cfdata_t, void *);
     99      1.1     tonio void uthum_attach(device_t, device_t, void *);
    100      1.1     tonio void uthum_childdet(device_t, device_t);
    101      1.1     tonio int uthum_detach(device_t, int);
    102      1.1     tonio int uthum_activate(device_t, enum devact);
    103      1.1     tonio 
    104      1.1     tonio int uthum_read_data(struct uthum_softc *, uint8_t, uint8_t *, size_t, int);
    105      1.1     tonio int uthum_check_sensortype(struct uthum_softc *);
    106  1.1.2.1  uebayasi int uthum_temper_temp(uint8_t, uint8_t);
    107  1.1.2.1  uebayasi int uthum_sht1x_temp(uint8_t, uint8_t);
    108      1.1     tonio int uthum_sht1x_rh(unsigned int, int);
    109      1.1     tonio 
    110      1.1     tonio void uthum_intr(struct uhidev *, void *, u_int);
    111      1.1     tonio static void uthum_refresh(struct sysmon_envsys *, envsys_data_t *);
    112      1.1     tonio 
    113      1.1     tonio extern struct cfdriver uthum_cd;
    114      1.1     tonio CFATTACH_DECL_NEW(uthum, sizeof(struct uthum_softc), uthum_match, uthum_attach,
    115      1.1     tonio     uthum_detach, uthum_activate);
    116      1.1     tonio 
    117      1.1     tonio USB_MATCH(uthum)
    118      1.1     tonio {
    119      1.1     tonio 	USB_MATCH_START(uthum, uaa);
    120      1.1     tonio 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa;
    121      1.1     tonio 
    122      1.1     tonio 	return (uthum_lookup(uha->uaa->vendor, uha->uaa->product) != NULL ?
    123      1.1     tonio 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    124      1.1     tonio }
    125      1.1     tonio 
    126      1.1     tonio USB_ATTACH(uthum)
    127      1.1     tonio {
    128      1.1     tonio 	USB_ATTACH_START(uthum, sc, uaa);
    129      1.1     tonio 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa;
    130      1.1     tonio 	usbd_device_handle dev = uha->parent->sc_udev;
    131      1.1     tonio 	int size, repid;
    132      1.1     tonio 	void *desc;
    133      1.1     tonio 
    134      1.1     tonio 	sc->sc_udev = dev;
    135      1.1     tonio 	sc->sc_hdev.sc_dev = self;
    136      1.1     tonio 	sc->sc_hdev.sc_intr = uthum_intr;
    137      1.1     tonio 	sc->sc_hdev.sc_parent = uha->parent;
    138      1.1     tonio 	sc->sc_hdev.sc_report_id = uha->reportid;
    139      1.1     tonio 	sc->sc_num_sensors = 0;
    140      1.1     tonio 
    141  1.1.2.1  uebayasi 	aprint_normal("\n");
    142  1.1.2.1  uebayasi 	aprint_naive("\n");
    143  1.1.2.1  uebayasi 
    144      1.1     tonio 	uhidev_get_report_desc(uha->parent, &desc, &size);
    145      1.1     tonio 	repid = uha->reportid;
    146      1.1     tonio 	sc->sc_ilen = hid_report_size(desc, size, hid_input, repid);
    147      1.1     tonio 	sc->sc_olen = hid_report_size(desc, size, hid_output, repid);
    148      1.1     tonio 	sc->sc_flen = hid_report_size(desc, size, hid_feature, repid);
    149      1.1     tonio 
    150      1.1     tonio 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    151      1.1     tonio 	    sc->sc_hdev.sc_dev);
    152      1.1     tonio 
    153      1.1     tonio 	if (sc->sc_flen < 32) {
    154      1.1     tonio 		/* not sensor interface, just attach */
    155      1.1     tonio 		return;
    156      1.1     tonio 	}
    157      1.1     tonio 
    158      1.1     tonio 	sc->sc_sensortype = uthum_check_sensortype(sc);
    159      1.1     tonio 
    160      1.1     tonio 	/* attach sensor */
    161      1.1     tonio 	sc->sc_sme = sysmon_envsys_create();
    162      1.1     tonio 	sc->sc_sme->sme_name = device_xname(self);
    163      1.1     tonio 
    164      1.1     tonio 	switch (sc->sc_sensortype) {
    165      1.1     tonio 	case UTHUM_TYPE_SHT1x:
    166      1.1     tonio 		(void)strlcpy(sc->sc_sensor[UTHUM_TEMP].desc, "temp",
    167      1.1     tonio 		    sizeof(sc->sc_sensor[UTHUM_TEMP].desc));
    168      1.1     tonio 		sc->sc_sensor[UTHUM_TEMP].units = ENVSYS_STEMP;
    169      1.1     tonio 		sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID;
    170      1.1     tonio 
    171  1.1.2.1  uebayasi 		(void)strlcpy(sc->sc_sensor[UTHUM_HUMIDITY].desc,
    172  1.1.2.1  uebayasi 		    "relative humidity",
    173      1.1     tonio 		    sizeof(sc->sc_sensor[UTHUM_HUMIDITY].desc));
    174      1.1     tonio 		sc->sc_sensor[UTHUM_HUMIDITY].units = ENVSYS_INTEGER;
    175      1.1     tonio 		sc->sc_sensor[UTHUM_HUMIDITY].value_cur = 0;
    176      1.1     tonio 		sc->sc_sensor[UTHUM_HUMIDITY].state = ENVSYS_SINVALID;
    177      1.1     tonio 
    178      1.1     tonio 		sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[UTHUM_TEMP]);
    179      1.1     tonio 		sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[UTHUM_HUMIDITY]);
    180      1.1     tonio 		sc->sc_num_sensors = 2;
    181      1.1     tonio 		DPRINTF(("sensor type: SHT1x\n"));
    182      1.1     tonio 		break;
    183      1.1     tonio 	case UTHUM_TYPE_TEMPER:
    184      1.1     tonio 		(void)strlcpy(sc->sc_sensor[UTHUM_TEMP].desc, "temp",
    185      1.1     tonio 		    sizeof(sc->sc_sensor[UTHUM_TEMP].desc));
    186      1.1     tonio 		sc->sc_sensor[UTHUM_TEMP].units = ENVSYS_STEMP;
    187      1.1     tonio 		sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID;
    188      1.1     tonio 
    189      1.1     tonio 		sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[UTHUM_TEMP]);
    190      1.1     tonio 		sc->sc_num_sensors = 1;
    191      1.1     tonio 		DPRINTF(("sensor type: TEMPer\n"));
    192      1.1     tonio 		break;
    193      1.1     tonio 	case UTHUM_TYPE_UNKNOWN:
    194      1.1     tonio 		DPRINTF(("sensor type: unknown, give up to attach sensors\n"));
    195      1.1     tonio 	default:
    196      1.1     tonio 		break;
    197      1.1     tonio 	}
    198      1.1     tonio 
    199      1.1     tonio 	if (sc->sc_num_sensors > 0) {
    200      1.1     tonio 		sc->sc_sme->sme_cookie = sc;
    201      1.1     tonio 		sc->sc_sme->sme_refresh = uthum_refresh;
    202      1.1     tonio 
    203      1.1     tonio 		if (sysmon_envsys_register(sc->sc_sme)) {
    204      1.1     tonio 			aprint_error_dev(self, "unable to register with sysmon\n");
    205      1.1     tonio 			sysmon_envsys_destroy(sc->sc_sme);
    206      1.1     tonio 		}
    207      1.1     tonio 	} else {
    208      1.1     tonio 		sysmon_envsys_destroy(sc->sc_sme);
    209      1.1     tonio 	}
    210      1.1     tonio 
    211      1.1     tonio 	DPRINTF(("uthum_attach: complete\n"));
    212      1.1     tonio }
    213      1.1     tonio 
    214      1.1     tonio USB_DETACH(uthum)
    215      1.1     tonio {
    216      1.1     tonio 	USB_DETACH_START(uthum, sc);
    217      1.1     tonio 	int rv = 0;
    218      1.1     tonio 
    219      1.1     tonio 	sc->sc_dying = 1;
    220      1.1     tonio 
    221      1.1     tonio 	if (sc->sc_num_sensors > 0) {
    222      1.1     tonio 		sysmon_envsys_unregister(sc->sc_sme);
    223      1.1     tonio 	}
    224      1.1     tonio 
    225      1.1     tonio 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    226      1.1     tonio 	    sc->sc_hdev.sc_dev);
    227      1.1     tonio 
    228      1.1     tonio 	return (rv);
    229      1.1     tonio }
    230      1.1     tonio 
    231      1.1     tonio int
    232      1.1     tonio uthum_activate(device_t self, enum devact act)
    233      1.1     tonio {
    234      1.1     tonio 	struct uthum_softc *sc = device_private(self);
    235      1.1     tonio 
    236      1.1     tonio 	switch (act) {
    237      1.1     tonio 	case DVACT_DEACTIVATE:
    238      1.1     tonio 		sc->sc_dying = 1;
    239      1.1     tonio 		break;
    240      1.1     tonio 	}
    241      1.1     tonio 	return (0);
    242      1.1     tonio }
    243      1.1     tonio 
    244      1.1     tonio void
    245      1.1     tonio uthum_intr(struct uhidev *addr, void *ibuf, u_int len)
    246      1.1     tonio {
    247      1.1     tonio 	/* do nothing */
    248      1.1     tonio }
    249      1.1     tonio 
    250      1.1     tonio int
    251      1.1     tonio uthum_read_data(struct uthum_softc *sc, uint8_t target_cmd, uint8_t *buf,
    252  1.1.2.1  uebayasi 	size_t len, int need_delay)
    253      1.1     tonio {
    254      1.1     tonio 	int i;
    255      1.1     tonio 	uint8_t cmdbuf[32], report[256];
    256      1.1     tonio 
    257      1.1     tonio 	/* if return buffer is null, do nothing */
    258      1.1     tonio 	if ((buf == NULL) || len == 0)
    259      1.1     tonio 		return 0;
    260      1.1     tonio 
    261      1.1     tonio 	/* issue query */
    262      1.1     tonio 	bzero(cmdbuf, sizeof(cmdbuf));
    263      1.1     tonio 	memcpy(cmdbuf, cmd_start, sizeof(cmd_start));
    264      1.1     tonio 	if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
    265      1.1     tonio 	    cmdbuf, sc->sc_olen))
    266      1.1     tonio 		return EIO;
    267      1.1     tonio 
    268      1.1     tonio 	bzero(cmdbuf, sizeof(cmdbuf));
    269      1.1     tonio 	cmdbuf[0] = target_cmd;
    270      1.1     tonio 	if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
    271      1.1     tonio 	    cmdbuf, sc->sc_olen))
    272      1.1     tonio 		return EIO;
    273      1.1     tonio 
    274      1.1     tonio 	bzero(cmdbuf, sizeof(cmdbuf));
    275      1.1     tonio 	for (i = 0; i < 7; i++) {
    276      1.1     tonio 		if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
    277      1.1     tonio 		    cmdbuf, sc->sc_olen))
    278      1.1     tonio 			return EIO;
    279      1.1     tonio 	}
    280      1.1     tonio 	memcpy(cmdbuf, cmd_end, sizeof(cmd_end));
    281      1.1     tonio 	if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
    282      1.1     tonio 	    cmdbuf, sc->sc_olen))
    283      1.1     tonio 		return EIO;
    284      1.1     tonio 
    285      1.1     tonio 	/* wait if required */
    286  1.1.2.1  uebayasi 	if (need_delay > 1)
    287  1.1.2.1  uebayasi 		tsleep(&sc->sc_sme, 0, "uthum", (need_delay*hz+999)/1000 + 1);
    288      1.1     tonio 
    289      1.1     tonio 	/* get answer */
    290      1.1     tonio 	if (uhidev_get_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
    291      1.1     tonio 	    report, sc->sc_flen))
    292      1.1     tonio 		return EIO;
    293      1.1     tonio 	memcpy(buf, report, len);
    294      1.1     tonio 	return 0;
    295      1.1     tonio }
    296      1.1     tonio 
    297      1.1     tonio int
    298      1.1     tonio uthum_check_sensortype(struct uthum_softc *sc)
    299      1.1     tonio {
    300      1.1     tonio 	uint8_t buf[8];
    301  1.1.2.1  uebayasi 	static uint8_t sht1x_sig0[] =
    302      1.1     tonio 	    { 0x57, 0x5a, 0x13, 0x00, 0x14, 0x00, 0x53, 0x00 };
    303  1.1.2.1  uebayasi 	static uint8_t sht1x_sig1[] =
    304  1.1.2.1  uebayasi 	    { 0x57, 0x5a, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 };
    305      1.1     tonio 	static uint8_t temper_sig[] =
    306      1.1     tonio 	    { 0x57, 0x58, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 };
    307      1.1     tonio 
    308      1.1     tonio 	if (uthum_read_data(sc, CMD_DEVTYPE, buf, sizeof(buf), 0) != 0) {
    309      1.1     tonio 		DPRINTF(("uthum: read fail\n"));
    310      1.1     tonio 		return UTHUM_TYPE_UNKNOWN;
    311      1.1     tonio 	}
    312      1.1     tonio 
    313      1.1     tonio 	/*
    314      1.1     tonio 	 * currently we have not enough information about the return value,
    315      1.1     tonio 	 * therefore, compare full bytes.
    316      1.1     tonio 	 * TEMPerHUM HID (SHT1x version) will return:
    317      1.1     tonio 	 *   { 0x57, 0x5a, 0x13, 0x00, 0x14, 0x00, 0x53, 0x00 }
    318  1.1.2.1  uebayasi 	 *   { 0x57, 0x5a, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 }
    319  1.1.2.1  uebayasi 	 * TEMPer HID (TEMPer version) will return:
    320      1.1     tonio 	 *   { 0x57, 0x58, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 }
    321      1.1     tonio 	 */
    322  1.1.2.1  uebayasi 	DPRINTF(("uthum: device signature: "
    323  1.1.2.1  uebayasi 	    "0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x\n",
    324  1.1.2.1  uebayasi 	    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]));
    325  1.1.2.1  uebayasi 	if (0 == memcmp(buf, sht1x_sig0, sizeof(sht1x_sig0)))
    326  1.1.2.1  uebayasi 		return UTHUM_TYPE_SHT1x;
    327  1.1.2.1  uebayasi 	if (0 == memcmp(buf, sht1x_sig1, sizeof(sht1x_sig1)))
    328      1.1     tonio 		return UTHUM_TYPE_SHT1x;
    329      1.1     tonio 	if (0 == memcmp(buf, temper_sig, sizeof(temper_sig)))
    330      1.1     tonio 		return UTHUM_TYPE_TEMPER;
    331      1.1     tonio 
    332      1.1     tonio 	return UTHUM_TYPE_UNKNOWN;
    333      1.1     tonio }
    334      1.1     tonio 
    335      1.1     tonio 
    336      1.1     tonio static void
    337      1.1     tonio uthum_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    338      1.1     tonio {
    339      1.1     tonio 	struct uthum_softc *sc = sme->sme_cookie;
    340      1.1     tonio 	uint8_t buf[8];
    341  1.1.2.1  uebayasi 	unsigned int humidity_tick;
    342      1.1     tonio 	int temp, rh;
    343      1.1     tonio 
    344      1.1     tonio 	switch (sc->sc_sensortype) {
    345      1.1     tonio 	case UTHUM_TYPE_SHT1x:
    346      1.1     tonio 		if (uthum_read_data(sc, CMD_GETDATA, buf, sizeof(buf), 1000) != 0) {
    347      1.1     tonio 			DPRINTF(("uthum: data read fail\n"));
    348      1.1     tonio 			sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID;
    349      1.1     tonio 			sc->sc_sensor[UTHUM_HUMIDITY].state = ENVSYS_SINVALID;
    350      1.1     tonio 			return;
    351      1.1     tonio 		}
    352  1.1.2.1  uebayasi 		DPRINTF(("%s: read SHT1x data "
    353  1.1.2.1  uebayasi 		    "0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x\n",
    354  1.1.2.1  uebayasi 		    sc->sc_sme->sme_name, buf[0], buf[1], buf[2], buf[3],
    355  1.1.2.1  uebayasi 		    buf[4], buf[5], buf[6], buf[7]));
    356      1.1     tonio 
    357      1.1     tonio 		humidity_tick = (buf[2] * 256 + buf[3]) & 0x0fff;
    358      1.1     tonio 
    359  1.1.2.1  uebayasi 		temp = uthum_sht1x_temp(buf[0], buf[1]);
    360      1.1     tonio 		rh = uthum_sht1x_rh(humidity_tick, temp);
    361      1.1     tonio 
    362  1.1.2.1  uebayasi 		sc->sc_sensor[UTHUM_HUMIDITY].value_cur = rh / 1000;
    363      1.1     tonio 		sc->sc_sensor[UTHUM_HUMIDITY].state = ENVSYS_SVALID;
    364      1.1     tonio 		break;
    365      1.1     tonio 	case UTHUM_TYPE_TEMPER:
    366      1.1     tonio 		if (uthum_read_data(sc, CMD_GETTEMP, buf, sizeof(buf), 0) != 0) {
    367      1.1     tonio 			DPRINTF(("uthum: data read fail\n"));
    368      1.1     tonio 			sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID;
    369      1.1     tonio 			return;
    370      1.1     tonio 		}
    371  1.1.2.1  uebayasi 		DPRINTF(("%s: read TEMPER data "
    372  1.1.2.1  uebayasi 		    "0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x\n",
    373  1.1.2.1  uebayasi 		    sc->sc_sme->sme_name, buf[0], buf[1], buf[2], buf[3],
    374  1.1.2.1  uebayasi 		    buf[4], buf[5], buf[6], buf[7]));
    375  1.1.2.1  uebayasi 		temp = uthum_temper_temp(buf[0], buf[1]);
    376      1.1     tonio 		break;
    377      1.1     tonio 	default:
    378      1.1     tonio 		/* do nothing */
    379      1.1     tonio 		return;
    380      1.1     tonio 	}
    381      1.1     tonio 
    382      1.1     tonio 	sc->sc_sensor[UTHUM_TEMP].value_cur = (temp * 10000) + 273150000;
    383      1.1     tonio 	sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SVALID;
    384      1.1     tonio }
    385      1.1     tonio 
    386      1.1     tonio /* return C-degree * 100 value */
    387      1.1     tonio int
    388  1.1.2.1  uebayasi uthum_temper_temp(uint8_t msb, uint8_t lsb)
    389  1.1.2.1  uebayasi {
    390  1.1.2.1  uebayasi 	int val;
    391  1.1.2.1  uebayasi 
    392  1.1.2.1  uebayasi 	val = (msb << 8) | lsb;
    393  1.1.2.1  uebayasi 	if (val >= 32768) {
    394  1.1.2.1  uebayasi 		val = val - 65536;
    395  1.1.2.1  uebayasi 	}
    396  1.1.2.1  uebayasi 	val = (val * 100) >> 8;
    397  1.1.2.1  uebayasi 	return val;
    398  1.1.2.1  uebayasi }
    399  1.1.2.1  uebayasi 
    400  1.1.2.1  uebayasi /* return C-degree * 100 value */
    401  1.1.2.1  uebayasi int
    402  1.1.2.1  uebayasi uthum_sht1x_temp(uint8_t msb, uint8_t lsb)
    403      1.1     tonio {
    404  1.1.2.1  uebayasi 	int val;
    405  1.1.2.1  uebayasi 
    406  1.1.2.1  uebayasi 	val = ((msb << 8) + lsb) - 4096;
    407  1.1.2.1  uebayasi 	return val;
    408      1.1     tonio }
    409      1.1     tonio 
    410      1.1     tonio /* return %RH * 1000 */
    411      1.1     tonio int
    412      1.1     tonio uthum_sht1x_rh(unsigned int ticks, int temp)
    413      1.1     tonio {
    414      1.1     tonio 	int rh_l, rh;
    415      1.1     tonio 
    416      1.1     tonio 	rh_l = (-40000 + 405 * ticks) - ((7 * ticks * ticks) / 250);
    417      1.1     tonio 	rh = ((temp - 2500) * (1 + (ticks >> 7)) + rh_l) / 10;
    418      1.1     tonio 	return rh;
    419      1.1     tonio }
    420