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