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