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