Home | History | Annotate | Line # | Download | only in usb
uthum.c revision 1.21
      1 /*	$NetBSD: uthum.c,v 1.21 2021/06/13 09:28:23 mlelstv 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.21 2021/06/13 09:28:23 mlelstv 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 			sysmon_envsys_destroy(sc->sc_sme);
    210 			sc->sc_sme = NULL;
    211 			aprint_error_dev(self,
    212 			    "unable to register with sysmon\n");
    213 		}
    214 	} else {
    215 		sysmon_envsys_destroy(sc->sc_sme);
    216 		sc->sc_sme = NULL;
    217 	}
    218 
    219 	DPRINTF(("uthum_attach: complete\n"));
    220 }
    221 
    222 static int
    223 uthum_detach(device_t self, int flags)
    224 {
    225 	struct uthum_softc *sc = device_private(self);
    226 	int rv = 0;
    227 
    228 	sc->sc_dying = 1;
    229 
    230 	if (sc->sc_sme != NULL)
    231 		sysmon_envsys_unregister(sc->sc_sme);
    232 
    233 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    234 	    sc->sc_hdev.sc_dev);
    235 
    236 	return rv;
    237 }
    238 
    239 static int
    240 uthum_activate(device_t self, enum devact act)
    241 {
    242 	struct uthum_softc *sc = device_private(self);
    243 
    244 	switch (act) {
    245 	case DVACT_DEACTIVATE:
    246 		sc->sc_dying = 1;
    247 		break;
    248 	}
    249 	return 0;
    250 }
    251 
    252 static void
    253 uthum_intr(struct uhidev *addr, void *ibuf, u_int len)
    254 {
    255 	/* do nothing */
    256 }
    257 
    258 static int
    259 uthum_read_data(struct uthum_softc *sc, uint8_t target_cmd, uint8_t *buf,
    260 	size_t len, int need_delay)
    261 {
    262 	int i;
    263 	uint8_t cmdbuf[32], report[256];
    264 	size_t olen, flen;
    265 
    266 	/* if return buffer is null, do nothing */
    267 	if ((buf == NULL) || len == 0)
    268 		return 0;
    269 
    270 	olen = uimin(sc->sc_olen, sizeof(cmdbuf));
    271 
    272 	/* issue query */
    273 	memset(cmdbuf, 0, sizeof(cmdbuf));
    274 	memcpy(cmdbuf, cmd_start, sizeof(cmd_start));
    275 	if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
    276 	    cmdbuf, olen))
    277 		return EIO;
    278 
    279 	memset(cmdbuf, 0, sizeof(cmdbuf));
    280 	cmdbuf[0] = target_cmd;
    281 	if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
    282 	    cmdbuf, olen))
    283 		return EIO;
    284 
    285 	memset(cmdbuf, 0, sizeof(cmdbuf));
    286 	for (i = 0; i < 7; i++) {
    287 		if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
    288 		    cmdbuf, olen))
    289 			return EIO;
    290 	}
    291 	memcpy(cmdbuf, cmd_end, sizeof(cmd_end));
    292 	if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
    293 	    cmdbuf, olen))
    294 		return EIO;
    295 
    296 	/* wait if required */
    297 	if (need_delay > 1)
    298 		kpause("uthum", false, (need_delay*hz+999)/1000 + 1, NULL);
    299 
    300 	/* get answer */
    301 	flen = uimin(sc->sc_flen, sizeof(report));
    302 	if (uhidev_get_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
    303 	    report, flen))
    304 		return EIO;
    305 	memcpy(buf, report, len);
    306 	return 0;
    307 }
    308 
    309 static int
    310 uthum_check_sensortype(struct uthum_softc *sc)
    311 {
    312 	uint8_t buf[8];
    313 	static uint8_t sht1x_sig0[] =
    314 	    { 0x57, 0x5a, 0x13, 0x00, 0x14, 0x00, 0x53, 0x00 };
    315 	static uint8_t sht1x_sig1[] =
    316 	    { 0x57, 0x5a, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 };
    317 	static uint8_t temper_sig[] =
    318 	    { 0x57, 0x58, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 };
    319 
    320 	if (uthum_read_data(sc, CMD_DEVTYPE, buf, sizeof(buf), 0) != 0) {
    321 		DPRINTF(("uthum: read fail\n"));
    322 		return UTHUM_TYPE_UNKNOWN;
    323 	}
    324 
    325 	/*
    326 	 * currently we have not enough information about the return value,
    327 	 * therefore, compare full bytes.
    328 	 * TEMPerHUM HID (SHT1x version) will return:
    329 	 *   { 0x57, 0x5a, 0x13, 0x00, 0x14, 0x00, 0x53, 0x00 }
    330 	 *   { 0x57, 0x5a, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 }
    331 	 * TEMPer HID (TEMPer version) will return:
    332 	 *   { 0x57, 0x58, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 }
    333 	 */
    334 	DPRINTF(("uthum: device signature: "
    335 	    "0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x\n",
    336 	    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]));
    337 	if (0 == memcmp(buf, sht1x_sig0, sizeof(sht1x_sig0)))
    338 		return UTHUM_TYPE_SHT1x;
    339 	if (0 == memcmp(buf, sht1x_sig1, sizeof(sht1x_sig1)))
    340 		return UTHUM_TYPE_SHT1x;
    341 	if (0 == memcmp(buf, temper_sig, sizeof(temper_sig)))
    342 		return UTHUM_TYPE_TEMPER;
    343 
    344 	return UTHUM_TYPE_UNKNOWN;
    345 }
    346 
    347 
    348 static void
    349 uthum_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    350 {
    351 	struct uthum_softc *sc = sme->sme_cookie;
    352 	uint8_t buf[8];
    353 	unsigned int humidity_tick;
    354 	int temp, rh;
    355 
    356 	switch (sc->sc_sensortype) {
    357 	case UTHUM_TYPE_SHT1x:
    358 		if (uthum_read_data(sc, CMD_GETDATA, buf, sizeof(buf), 1000)
    359 		    != 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 static 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 static 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 static 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