1 /* $NetBSD: uthum.c,v 1.2 2010/02/24 17:34:56 plunky 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 /* Driver for TEMPer and TEMPerHUM HID */ 21 22 #include <sys/param.h> 23 #include <sys/proc.h> 24 #include <sys/systm.h> 25 #include <sys/kernel.h> 26 #include <sys/malloc.h> 27 #include <sys/device.h> 28 #include <sys/conf.h> 29 30 #include <dev/sysmon/sysmonvar.h> 31 32 #include <dev/usb/usb.h> 33 #include <dev/usb/usbhid.h> 34 #include <dev/usb/usbdi.h> 35 #include <dev/usb/usbdi_util.h> 36 #include <dev/usb/usbdevs.h> 37 #include <dev/usb/uhidev.h> 38 #include <dev/usb/hid.h> 39 40 #ifdef USB_DEBUG 41 #define UTHUM_DEBUG 42 #endif 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 usbd_device_handle 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_sht1x_temp(unsigned int); 107 int uthum_sht1x_rh(unsigned int, int); 108 109 void uthum_intr(struct uhidev *, void *, u_int); 110 static void uthum_refresh(struct sysmon_envsys *, envsys_data_t *); 111 112 extern struct cfdriver uthum_cd; 113 CFATTACH_DECL_NEW(uthum, sizeof(struct uthum_softc), uthum_match, uthum_attach, 114 uthum_detach, uthum_activate); 115 116 USB_MATCH(uthum) 117 { 118 USB_MATCH_START(uthum, uaa); 119 struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa; 120 121 return (uthum_lookup(uha->uaa->vendor, uha->uaa->product) != NULL ? 122 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 123 } 124 125 USB_ATTACH(uthum) 126 { 127 USB_ATTACH_START(uthum, sc, uaa); 128 struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa; 129 usbd_device_handle dev = uha->parent->sc_udev; 130 int size, repid; 131 void *desc; 132 133 sc->sc_udev = dev; 134 sc->sc_hdev.sc_dev = self; 135 sc->sc_hdev.sc_intr = uthum_intr; 136 sc->sc_hdev.sc_parent = uha->parent; 137 sc->sc_hdev.sc_report_id = uha->reportid; 138 sc->sc_num_sensors = 0; 139 140 aprint_normal("\n"); 141 aprint_naive("\n"); 142 143 uhidev_get_report_desc(uha->parent, &desc, &size); 144 repid = uha->reportid; 145 sc->sc_ilen = hid_report_size(desc, size, hid_input, repid); 146 sc->sc_olen = hid_report_size(desc, size, hid_output, repid); 147 sc->sc_flen = hid_report_size(desc, size, hid_feature, repid); 148 149 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 150 sc->sc_hdev.sc_dev); 151 152 if (sc->sc_flen < 32) { 153 /* not sensor interface, just attach */ 154 return; 155 } 156 157 sc->sc_sensortype = uthum_check_sensortype(sc); 158 159 /* attach sensor */ 160 sc->sc_sme = sysmon_envsys_create(); 161 sc->sc_sme->sme_name = device_xname(self); 162 163 switch (sc->sc_sensortype) { 164 case UTHUM_TYPE_SHT1x: 165 (void)strlcpy(sc->sc_sensor[UTHUM_TEMP].desc, "temp", 166 sizeof(sc->sc_sensor[UTHUM_TEMP].desc)); 167 sc->sc_sensor[UTHUM_TEMP].units = ENVSYS_STEMP; 168 sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID; 169 170 (void)strlcpy(sc->sc_sensor[UTHUM_HUMIDITY].desc, "humidity", 171 sizeof(sc->sc_sensor[UTHUM_HUMIDITY].desc)); 172 sc->sc_sensor[UTHUM_HUMIDITY].units = ENVSYS_INTEGER; 173 sc->sc_sensor[UTHUM_HUMIDITY].value_cur = 0; 174 sc->sc_sensor[UTHUM_HUMIDITY].state = ENVSYS_SINVALID; 175 176 sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[UTHUM_TEMP]); 177 sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[UTHUM_HUMIDITY]); 178 sc->sc_num_sensors = 2; 179 DPRINTF(("sensor type: SHT1x\n")); 180 break; 181 case UTHUM_TYPE_TEMPER: 182 (void)strlcpy(sc->sc_sensor[UTHUM_TEMP].desc, "temp", 183 sizeof(sc->sc_sensor[UTHUM_TEMP].desc)); 184 sc->sc_sensor[UTHUM_TEMP].units = ENVSYS_STEMP; 185 sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID; 186 187 sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[UTHUM_TEMP]); 188 sc->sc_num_sensors = 1; 189 DPRINTF(("sensor type: TEMPer\n")); 190 break; 191 case UTHUM_TYPE_UNKNOWN: 192 DPRINTF(("sensor type: unknown, give up to attach sensors\n")); 193 default: 194 break; 195 } 196 197 if (sc->sc_num_sensors > 0) { 198 sc->sc_sme->sme_cookie = sc; 199 sc->sc_sme->sme_refresh = uthum_refresh; 200 201 if (sysmon_envsys_register(sc->sc_sme)) { 202 aprint_error_dev(self, "unable to register with sysmon\n"); 203 sysmon_envsys_destroy(sc->sc_sme); 204 } 205 } else { 206 sysmon_envsys_destroy(sc->sc_sme); 207 } 208 209 DPRINTF(("uthum_attach: complete\n")); 210 } 211 212 USB_DETACH(uthum) 213 { 214 USB_DETACH_START(uthum, sc); 215 int rv = 0; 216 217 sc->sc_dying = 1; 218 219 if (sc->sc_num_sensors > 0) { 220 sysmon_envsys_unregister(sc->sc_sme); 221 } 222 223 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 224 sc->sc_hdev.sc_dev); 225 226 return (rv); 227 } 228 229 int 230 uthum_activate(device_t self, enum devact act) 231 { 232 struct uthum_softc *sc = device_private(self); 233 234 switch (act) { 235 case DVACT_DEACTIVATE: 236 sc->sc_dying = 1; 237 break; 238 } 239 return (0); 240 } 241 242 void 243 uthum_intr(struct uhidev *addr, void *ibuf, u_int len) 244 { 245 /* do nothing */ 246 } 247 248 int 249 uthum_read_data(struct uthum_softc *sc, uint8_t target_cmd, uint8_t *buf, 250 size_t len, int delay) 251 { 252 int i; 253 uint8_t cmdbuf[32], report[256]; 254 255 /* if return buffer is null, do nothing */ 256 if ((buf == NULL) || len == 0) 257 return 0; 258 259 /* issue query */ 260 bzero(cmdbuf, sizeof(cmdbuf)); 261 memcpy(cmdbuf, cmd_start, sizeof(cmd_start)); 262 if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, 263 cmdbuf, sc->sc_olen)) 264 return EIO; 265 266 bzero(cmdbuf, sizeof(cmdbuf)); 267 cmdbuf[0] = target_cmd; 268 if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, 269 cmdbuf, sc->sc_olen)) 270 return EIO; 271 272 bzero(cmdbuf, sizeof(cmdbuf)); 273 for (i = 0; i < 7; i++) { 274 if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, 275 cmdbuf, sc->sc_olen)) 276 return EIO; 277 } 278 memcpy(cmdbuf, cmd_end, sizeof(cmd_end)); 279 if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, 280 cmdbuf, sc->sc_olen)) 281 return EIO; 282 283 /* wait if required */ 284 if (delay > 1) 285 tsleep(&sc->sc_sme, 0, "uthum", (delay*hz+999)/1000 + 1); 286 287 /* get answer */ 288 if (uhidev_get_report(&sc->sc_hdev, UHID_FEATURE_REPORT, 289 report, sc->sc_flen)) 290 return EIO; 291 memcpy(buf, report, len); 292 return 0; 293 } 294 295 int 296 uthum_check_sensortype(struct uthum_softc *sc) 297 { 298 uint8_t buf[8]; 299 static uint8_t sht1x_sig[] = 300 { 0x57, 0x5a, 0x13, 0x00, 0x14, 0x00, 0x53, 0x00 }; 301 static uint8_t temper_sig[] = 302 { 0x57, 0x58, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 }; 303 304 if (uthum_read_data(sc, CMD_DEVTYPE, buf, sizeof(buf), 0) != 0) { 305 DPRINTF(("uthum: read fail\n")); 306 return UTHUM_TYPE_UNKNOWN; 307 } 308 309 /* 310 * currently we have not enough information about the return value, 311 * therefore, compare full bytes. 312 * TEMPerHUM HID (SHT1x version) will return: 313 * { 0x57, 0x5a, 0x13, 0x00, 0x14, 0x00, 0x53, 0x00 } 314 * TEMPer HID (SHT1x version) will return: 315 * { 0x57, 0x58, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 } 316 */ 317 if (0 == memcmp(buf, sht1x_sig, sizeof(sht1x_sig))) 318 return UTHUM_TYPE_SHT1x; 319 if (0 == memcmp(buf, temper_sig, sizeof(temper_sig))) 320 return UTHUM_TYPE_TEMPER; 321 322 return UTHUM_TYPE_UNKNOWN; 323 } 324 325 326 static void 327 uthum_refresh(struct sysmon_envsys *sme, envsys_data_t *edata) 328 { 329 struct uthum_softc *sc = sme->sme_cookie; 330 uint8_t buf[8]; 331 unsigned int temp_tick, humidity_tick; 332 int temp, rh; 333 334 switch (sc->sc_sensortype) { 335 case UTHUM_TYPE_SHT1x: 336 if (uthum_read_data(sc, CMD_GETDATA, buf, sizeof(buf), 1000) != 0) { 337 DPRINTF(("uthum: data read fail\n")); 338 sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID; 339 sc->sc_sensor[UTHUM_HUMIDITY].state = ENVSYS_SINVALID; 340 return; 341 } 342 343 temp_tick = (buf[0] * 256 + buf[1]) & 0x3fff; 344 humidity_tick = (buf[2] * 256 + buf[3]) & 0x0fff; 345 346 temp = uthum_sht1x_temp(temp_tick); 347 rh = uthum_sht1x_rh(humidity_tick, temp); 348 349 sc->sc_sensor[UTHUM_HUMIDITY].value_cur = rh; 350 sc->sc_sensor[UTHUM_HUMIDITY].state = ENVSYS_SVALID; 351 break; 352 case UTHUM_TYPE_TEMPER: 353 if (uthum_read_data(sc, CMD_GETTEMP, buf, sizeof(buf), 0) != 0) { 354 DPRINTF(("uthum: data read fail\n")); 355 sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID; 356 return; 357 } 358 temp_tick = (buf[0] * 256 + buf[1]) & 0xffff; 359 temp = uthum_sht1x_temp(temp_tick); 360 break; 361 default: 362 /* do nothing */ 363 return; 364 } 365 366 sc->sc_sensor[UTHUM_TEMP].value_cur = (temp * 10000) + 273150000; 367 sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SVALID; 368 } 369 370 /* return C-degree * 100 value */ 371 int 372 uthum_sht1x_temp(unsigned int ticks) 373 { 374 return (ticks - 4010); 375 } 376 377 /* return %RH * 1000 */ 378 int 379 uthum_sht1x_rh(unsigned int ticks, int temp) 380 { 381 int rh_l, rh; 382 383 rh_l = (-40000 + 405 * ticks) - ((7 * ticks * ticks) / 250); 384 rh = ((temp - 2500) * (1 + (ticks >> 7)) + rh_l) / 10; 385 return rh; 386 } 387