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