uthum.c revision 1.4.2.2 1 1.4.2.2 yamt /* $NetBSD: uthum.c,v 1.4.2.2 2010/03/11 15:04:08 yamt Exp $ */
2 1.4.2.2 yamt /* $OpenBSD: uthum.c,v 1.6 2010/01/03 18:43:02 deraadt Exp $ */
3 1.4.2.2 yamt
4 1.4.2.2 yamt /*
5 1.4.2.2 yamt * Copyright (c) 2009 Yojiro UO <yuo (at) nui.org>
6 1.4.2.2 yamt *
7 1.4.2.2 yamt * Permission to use, copy, modify, and distribute this software for any
8 1.4.2.2 yamt * purpose with or without fee is hereby granted, provided that the above
9 1.4.2.2 yamt * copyright notice and this permission notice appear in all copies.
10 1.4.2.2 yamt *
11 1.4.2.2 yamt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCAIMS ALL WARRANTIES
12 1.4.2.2 yamt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.4.2.2 yamt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.4.2.2 yamt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.4.2.2 yamt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.4.2.2 yamt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.4.2.2 yamt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.4.2.2 yamt */
19 1.4.2.2 yamt
20 1.4.2.2 yamt /* Driver for TEMPer and TEMPerHUM HID */
21 1.4.2.2 yamt
22 1.4.2.2 yamt #include <sys/param.h>
23 1.4.2.2 yamt #include <sys/proc.h>
24 1.4.2.2 yamt #include <sys/systm.h>
25 1.4.2.2 yamt #include <sys/kernel.h>
26 1.4.2.2 yamt #include <sys/malloc.h>
27 1.4.2.2 yamt #include <sys/device.h>
28 1.4.2.2 yamt #include <sys/conf.h>
29 1.4.2.2 yamt
30 1.4.2.2 yamt #include <dev/sysmon/sysmonvar.h>
31 1.4.2.2 yamt
32 1.4.2.2 yamt #include <dev/usb/usb.h>
33 1.4.2.2 yamt #include <dev/usb/usbhid.h>
34 1.4.2.2 yamt #include <dev/usb/usbdi.h>
35 1.4.2.2 yamt #include <dev/usb/usbdi_util.h>
36 1.4.2.2 yamt #include <dev/usb/usbdevs.h>
37 1.4.2.2 yamt #include <dev/usb/uhidev.h>
38 1.4.2.2 yamt #include <dev/usb/hid.h>
39 1.4.2.2 yamt
40 1.4.2.2 yamt #ifdef USB_DEBUG
41 1.4.2.2 yamt #define UTHUM_DEBUG
42 1.4.2.2 yamt #endif
43 1.4.2.2 yamt
44 1.4.2.2 yamt #ifdef UTHUM_DEBUG
45 1.4.2.2 yamt int uthumdebug = 0;
46 1.4.2.2 yamt #define DPRINTFN(n, x) do { if (uthumdebug > (n)) printf x; } while (0)
47 1.4.2.2 yamt #else
48 1.4.2.2 yamt #define DPRINTFN(n, x)
49 1.4.2.2 yamt #endif
50 1.4.2.2 yamt
51 1.4.2.2 yamt #define DPRINTF(x) DPRINTFN(0, x)
52 1.4.2.2 yamt
53 1.4.2.2 yamt
54 1.4.2.2 yamt /* TEMPerHUM */
55 1.4.2.2 yamt #define CMD_DEVTYPE 0x52 /* XXX */
56 1.4.2.2 yamt #define CMD_GETDATA 0x48 /* XXX */
57 1.4.2.2 yamt #define CMD_GETTEMP 0x54 /* XXX */
58 1.4.2.2 yamt static uint8_t cmd_start[8] =
59 1.4.2.2 yamt { 0x0a, 0x0b, 0x0c, 0x0d, 0x00, 0x00, 0x02, 0x00 };
60 1.4.2.2 yamt static uint8_t cmd_end[8] =
61 1.4.2.2 yamt { 0x0a, 0x0b, 0x0c, 0x0d, 0x00, 0x00, 0x01, 0x00 };
62 1.4.2.2 yamt
63 1.4.2.2 yamt /* sensors */
64 1.4.2.2 yamt #define UTHUM_TEMP 0
65 1.4.2.2 yamt #define UTHUM_HUMIDITY 1
66 1.4.2.2 yamt #define UTHUM_MAX_SENSORS 2
67 1.4.2.2 yamt
68 1.4.2.2 yamt #define UTHUM_TYPE_UNKNOWN 0
69 1.4.2.2 yamt #define UTHUM_TYPE_SHT1x 1
70 1.4.2.2 yamt #define UTHUM_TYPE_TEMPER 2
71 1.4.2.2 yamt
72 1.4.2.2 yamt struct uthum_softc {
73 1.4.2.2 yamt struct uhidev sc_hdev;
74 1.4.2.2 yamt usbd_device_handle sc_udev;
75 1.4.2.2 yamt u_char sc_dying;
76 1.4.2.2 yamt uint16_t sc_flag;
77 1.4.2.2 yamt int sc_sensortype;
78 1.4.2.2 yamt
79 1.4.2.2 yamt /* uhidev parameters */
80 1.4.2.2 yamt size_t sc_flen; /* feature report length */
81 1.4.2.2 yamt size_t sc_ilen; /* input report length */
82 1.4.2.2 yamt size_t sc_olen; /* output report length */
83 1.4.2.2 yamt
84 1.4.2.2 yamt /* sensor framework */
85 1.4.2.2 yamt struct sysmon_envsys *sc_sme;
86 1.4.2.2 yamt envsys_data_t sc_sensor[UTHUM_MAX_SENSORS];
87 1.4.2.2 yamt
88 1.4.2.2 yamt uint8_t sc_num_sensors;
89 1.4.2.2 yamt };
90 1.4.2.2 yamt
91 1.4.2.2 yamt
92 1.4.2.2 yamt const struct usb_devno uthum_devs[] = {
93 1.4.2.2 yamt /* XXX: various TEMPer variants using same VID/PID */
94 1.4.2.2 yamt { USB_VENDOR_TENX, USB_PRODUCT_TENX_TEMPER},
95 1.4.2.2 yamt };
96 1.4.2.2 yamt #define uthum_lookup(v, p) usb_lookup(uthum_devs, v, p)
97 1.4.2.2 yamt
98 1.4.2.2 yamt int uthum_match(device_t, cfdata_t, void *);
99 1.4.2.2 yamt void uthum_attach(device_t, device_t, void *);
100 1.4.2.2 yamt void uthum_childdet(device_t, device_t);
101 1.4.2.2 yamt int uthum_detach(device_t, int);
102 1.4.2.2 yamt int uthum_activate(device_t, enum devact);
103 1.4.2.2 yamt
104 1.4.2.2 yamt int uthum_read_data(struct uthum_softc *, uint8_t, uint8_t *, size_t, int);
105 1.4.2.2 yamt int uthum_check_sensortype(struct uthum_softc *);
106 1.4.2.2 yamt int uthum_sht1x_temp(unsigned int);
107 1.4.2.2 yamt int uthum_sht1x_rh(unsigned int, int);
108 1.4.2.2 yamt
109 1.4.2.2 yamt void uthum_intr(struct uhidev *, void *, u_int);
110 1.4.2.2 yamt static void uthum_refresh(struct sysmon_envsys *, envsys_data_t *);
111 1.4.2.2 yamt
112 1.4.2.2 yamt extern struct cfdriver uthum_cd;
113 1.4.2.2 yamt CFATTACH_DECL_NEW(uthum, sizeof(struct uthum_softc), uthum_match, uthum_attach,
114 1.4.2.2 yamt uthum_detach, uthum_activate);
115 1.4.2.2 yamt
116 1.4.2.2 yamt USB_MATCH(uthum)
117 1.4.2.2 yamt {
118 1.4.2.2 yamt USB_MATCH_START(uthum, uaa);
119 1.4.2.2 yamt struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa;
120 1.4.2.2 yamt
121 1.4.2.2 yamt return (uthum_lookup(uha->uaa->vendor, uha->uaa->product) != NULL ?
122 1.4.2.2 yamt UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
123 1.4.2.2 yamt }
124 1.4.2.2 yamt
125 1.4.2.2 yamt USB_ATTACH(uthum)
126 1.4.2.2 yamt {
127 1.4.2.2 yamt USB_ATTACH_START(uthum, sc, uaa);
128 1.4.2.2 yamt struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa;
129 1.4.2.2 yamt usbd_device_handle dev = uha->parent->sc_udev;
130 1.4.2.2 yamt int size, repid;
131 1.4.2.2 yamt void *desc;
132 1.4.2.2 yamt
133 1.4.2.2 yamt sc->sc_udev = dev;
134 1.4.2.2 yamt sc->sc_hdev.sc_dev = self;
135 1.4.2.2 yamt sc->sc_hdev.sc_intr = uthum_intr;
136 1.4.2.2 yamt sc->sc_hdev.sc_parent = uha->parent;
137 1.4.2.2 yamt sc->sc_hdev.sc_report_id = uha->reportid;
138 1.4.2.2 yamt sc->sc_num_sensors = 0;
139 1.4.2.2 yamt
140 1.4.2.2 yamt aprint_normal("\n");
141 1.4.2.2 yamt aprint_naive("\n");
142 1.4.2.2 yamt
143 1.4.2.2 yamt uhidev_get_report_desc(uha->parent, &desc, &size);
144 1.4.2.2 yamt repid = uha->reportid;
145 1.4.2.2 yamt sc->sc_ilen = hid_report_size(desc, size, hid_input, repid);
146 1.4.2.2 yamt sc->sc_olen = hid_report_size(desc, size, hid_output, repid);
147 1.4.2.2 yamt sc->sc_flen = hid_report_size(desc, size, hid_feature, repid);
148 1.4.2.2 yamt
149 1.4.2.2 yamt usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
150 1.4.2.2 yamt sc->sc_hdev.sc_dev);
151 1.4.2.2 yamt
152 1.4.2.2 yamt if (sc->sc_flen < 32) {
153 1.4.2.2 yamt /* not sensor interface, just attach */
154 1.4.2.2 yamt return;
155 1.4.2.2 yamt }
156 1.4.2.2 yamt
157 1.4.2.2 yamt sc->sc_sensortype = uthum_check_sensortype(sc);
158 1.4.2.2 yamt
159 1.4.2.2 yamt /* attach sensor */
160 1.4.2.2 yamt sc->sc_sme = sysmon_envsys_create();
161 1.4.2.2 yamt sc->sc_sme->sme_name = device_xname(self);
162 1.4.2.2 yamt
163 1.4.2.2 yamt switch (sc->sc_sensortype) {
164 1.4.2.2 yamt case UTHUM_TYPE_SHT1x:
165 1.4.2.2 yamt (void)strlcpy(sc->sc_sensor[UTHUM_TEMP].desc, "temp",
166 1.4.2.2 yamt sizeof(sc->sc_sensor[UTHUM_TEMP].desc));
167 1.4.2.2 yamt sc->sc_sensor[UTHUM_TEMP].units = ENVSYS_STEMP;
168 1.4.2.2 yamt sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID;
169 1.4.2.2 yamt
170 1.4.2.2 yamt (void)strlcpy(sc->sc_sensor[UTHUM_HUMIDITY].desc,
171 1.4.2.2 yamt "relative humidity",
172 1.4.2.2 yamt sizeof(sc->sc_sensor[UTHUM_HUMIDITY].desc));
173 1.4.2.2 yamt sc->sc_sensor[UTHUM_HUMIDITY].units = ENVSYS_INTEGER;
174 1.4.2.2 yamt sc->sc_sensor[UTHUM_HUMIDITY].value_cur = 0;
175 1.4.2.2 yamt sc->sc_sensor[UTHUM_HUMIDITY].state = ENVSYS_SINVALID;
176 1.4.2.2 yamt
177 1.4.2.2 yamt sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[UTHUM_TEMP]);
178 1.4.2.2 yamt sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[UTHUM_HUMIDITY]);
179 1.4.2.2 yamt sc->sc_num_sensors = 2;
180 1.4.2.2 yamt DPRINTF(("sensor type: SHT1x\n"));
181 1.4.2.2 yamt break;
182 1.4.2.2 yamt case UTHUM_TYPE_TEMPER:
183 1.4.2.2 yamt (void)strlcpy(sc->sc_sensor[UTHUM_TEMP].desc, "temp",
184 1.4.2.2 yamt sizeof(sc->sc_sensor[UTHUM_TEMP].desc));
185 1.4.2.2 yamt sc->sc_sensor[UTHUM_TEMP].units = ENVSYS_STEMP;
186 1.4.2.2 yamt sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID;
187 1.4.2.2 yamt
188 1.4.2.2 yamt sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[UTHUM_TEMP]);
189 1.4.2.2 yamt sc->sc_num_sensors = 1;
190 1.4.2.2 yamt DPRINTF(("sensor type: TEMPer\n"));
191 1.4.2.2 yamt break;
192 1.4.2.2 yamt case UTHUM_TYPE_UNKNOWN:
193 1.4.2.2 yamt DPRINTF(("sensor type: unknown, give up to attach sensors\n"));
194 1.4.2.2 yamt default:
195 1.4.2.2 yamt break;
196 1.4.2.2 yamt }
197 1.4.2.2 yamt
198 1.4.2.2 yamt if (sc->sc_num_sensors > 0) {
199 1.4.2.2 yamt sc->sc_sme->sme_cookie = sc;
200 1.4.2.2 yamt sc->sc_sme->sme_refresh = uthum_refresh;
201 1.4.2.2 yamt
202 1.4.2.2 yamt if (sysmon_envsys_register(sc->sc_sme)) {
203 1.4.2.2 yamt aprint_error_dev(self, "unable to register with sysmon\n");
204 1.4.2.2 yamt sysmon_envsys_destroy(sc->sc_sme);
205 1.4.2.2 yamt }
206 1.4.2.2 yamt } else {
207 1.4.2.2 yamt sysmon_envsys_destroy(sc->sc_sme);
208 1.4.2.2 yamt }
209 1.4.2.2 yamt
210 1.4.2.2 yamt DPRINTF(("uthum_attach: complete\n"));
211 1.4.2.2 yamt }
212 1.4.2.2 yamt
213 1.4.2.2 yamt USB_DETACH(uthum)
214 1.4.2.2 yamt {
215 1.4.2.2 yamt USB_DETACH_START(uthum, sc);
216 1.4.2.2 yamt int rv = 0;
217 1.4.2.2 yamt
218 1.4.2.2 yamt sc->sc_dying = 1;
219 1.4.2.2 yamt
220 1.4.2.2 yamt if (sc->sc_num_sensors > 0) {
221 1.4.2.2 yamt sysmon_envsys_unregister(sc->sc_sme);
222 1.4.2.2 yamt }
223 1.4.2.2 yamt
224 1.4.2.2 yamt usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
225 1.4.2.2 yamt sc->sc_hdev.sc_dev);
226 1.4.2.2 yamt
227 1.4.2.2 yamt return (rv);
228 1.4.2.2 yamt }
229 1.4.2.2 yamt
230 1.4.2.2 yamt int
231 1.4.2.2 yamt uthum_activate(device_t self, enum devact act)
232 1.4.2.2 yamt {
233 1.4.2.2 yamt struct uthum_softc *sc = device_private(self);
234 1.4.2.2 yamt
235 1.4.2.2 yamt switch (act) {
236 1.4.2.2 yamt case DVACT_DEACTIVATE:
237 1.4.2.2 yamt sc->sc_dying = 1;
238 1.4.2.2 yamt break;
239 1.4.2.2 yamt }
240 1.4.2.2 yamt return (0);
241 1.4.2.2 yamt }
242 1.4.2.2 yamt
243 1.4.2.2 yamt void
244 1.4.2.2 yamt uthum_intr(struct uhidev *addr, void *ibuf, u_int len)
245 1.4.2.2 yamt {
246 1.4.2.2 yamt /* do nothing */
247 1.4.2.2 yamt }
248 1.4.2.2 yamt
249 1.4.2.2 yamt int
250 1.4.2.2 yamt uthum_read_data(struct uthum_softc *sc, uint8_t target_cmd, uint8_t *buf,
251 1.4.2.2 yamt size_t len, int need_delay)
252 1.4.2.2 yamt {
253 1.4.2.2 yamt int i;
254 1.4.2.2 yamt uint8_t cmdbuf[32], report[256];
255 1.4.2.2 yamt
256 1.4.2.2 yamt /* if return buffer is null, do nothing */
257 1.4.2.2 yamt if ((buf == NULL) || len == 0)
258 1.4.2.2 yamt return 0;
259 1.4.2.2 yamt
260 1.4.2.2 yamt /* issue query */
261 1.4.2.2 yamt bzero(cmdbuf, sizeof(cmdbuf));
262 1.4.2.2 yamt memcpy(cmdbuf, cmd_start, sizeof(cmd_start));
263 1.4.2.2 yamt if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
264 1.4.2.2 yamt cmdbuf, sc->sc_olen))
265 1.4.2.2 yamt return EIO;
266 1.4.2.2 yamt
267 1.4.2.2 yamt bzero(cmdbuf, sizeof(cmdbuf));
268 1.4.2.2 yamt cmdbuf[0] = target_cmd;
269 1.4.2.2 yamt if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
270 1.4.2.2 yamt cmdbuf, sc->sc_olen))
271 1.4.2.2 yamt return EIO;
272 1.4.2.2 yamt
273 1.4.2.2 yamt bzero(cmdbuf, sizeof(cmdbuf));
274 1.4.2.2 yamt for (i = 0; i < 7; i++) {
275 1.4.2.2 yamt if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
276 1.4.2.2 yamt cmdbuf, sc->sc_olen))
277 1.4.2.2 yamt return EIO;
278 1.4.2.2 yamt }
279 1.4.2.2 yamt memcpy(cmdbuf, cmd_end, sizeof(cmd_end));
280 1.4.2.2 yamt if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
281 1.4.2.2 yamt cmdbuf, sc->sc_olen))
282 1.4.2.2 yamt return EIO;
283 1.4.2.2 yamt
284 1.4.2.2 yamt /* wait if required */
285 1.4.2.2 yamt if (need_delay > 1)
286 1.4.2.2 yamt tsleep(&sc->sc_sme, 0, "uthum", (need_delay*hz+999)/1000 + 1);
287 1.4.2.2 yamt
288 1.4.2.2 yamt /* get answer */
289 1.4.2.2 yamt if (uhidev_get_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
290 1.4.2.2 yamt report, sc->sc_flen))
291 1.4.2.2 yamt return EIO;
292 1.4.2.2 yamt memcpy(buf, report, len);
293 1.4.2.2 yamt return 0;
294 1.4.2.2 yamt }
295 1.4.2.2 yamt
296 1.4.2.2 yamt int
297 1.4.2.2 yamt uthum_check_sensortype(struct uthum_softc *sc)
298 1.4.2.2 yamt {
299 1.4.2.2 yamt uint8_t buf[8];
300 1.4.2.2 yamt static uint8_t sht1x_sig[] =
301 1.4.2.2 yamt { 0x57, 0x5a, 0x13, 0x00, 0x14, 0x00, 0x53, 0x00 };
302 1.4.2.2 yamt static uint8_t temper_sig[] =
303 1.4.2.2 yamt { 0x57, 0x58, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 };
304 1.4.2.2 yamt
305 1.4.2.2 yamt if (uthum_read_data(sc, CMD_DEVTYPE, buf, sizeof(buf), 0) != 0) {
306 1.4.2.2 yamt DPRINTF(("uthum: read fail\n"));
307 1.4.2.2 yamt return UTHUM_TYPE_UNKNOWN;
308 1.4.2.2 yamt }
309 1.4.2.2 yamt
310 1.4.2.2 yamt /*
311 1.4.2.2 yamt * currently we have not enough information about the return value,
312 1.4.2.2 yamt * therefore, compare full bytes.
313 1.4.2.2 yamt * TEMPerHUM HID (SHT1x version) will return:
314 1.4.2.2 yamt * { 0x57, 0x5a, 0x13, 0x00, 0x14, 0x00, 0x53, 0x00 }
315 1.4.2.2 yamt * TEMPer HID (SHT1x version) will return:
316 1.4.2.2 yamt * { 0x57, 0x58, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 }
317 1.4.2.2 yamt */
318 1.4.2.2 yamt if (0 == memcmp(buf, sht1x_sig, sizeof(sht1x_sig)))
319 1.4.2.2 yamt return UTHUM_TYPE_SHT1x;
320 1.4.2.2 yamt if (0 == memcmp(buf, temper_sig, sizeof(temper_sig)))
321 1.4.2.2 yamt return UTHUM_TYPE_TEMPER;
322 1.4.2.2 yamt
323 1.4.2.2 yamt return UTHUM_TYPE_UNKNOWN;
324 1.4.2.2 yamt }
325 1.4.2.2 yamt
326 1.4.2.2 yamt
327 1.4.2.2 yamt static void
328 1.4.2.2 yamt uthum_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
329 1.4.2.2 yamt {
330 1.4.2.2 yamt struct uthum_softc *sc = sme->sme_cookie;
331 1.4.2.2 yamt uint8_t buf[8];
332 1.4.2.2 yamt unsigned int temp_tick, humidity_tick;
333 1.4.2.2 yamt int temp, rh;
334 1.4.2.2 yamt
335 1.4.2.2 yamt switch (sc->sc_sensortype) {
336 1.4.2.2 yamt case UTHUM_TYPE_SHT1x:
337 1.4.2.2 yamt if (uthum_read_data(sc, CMD_GETDATA, buf, sizeof(buf), 1000) != 0) {
338 1.4.2.2 yamt DPRINTF(("uthum: data read fail\n"));
339 1.4.2.2 yamt sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID;
340 1.4.2.2 yamt sc->sc_sensor[UTHUM_HUMIDITY].state = ENVSYS_SINVALID;
341 1.4.2.2 yamt return;
342 1.4.2.2 yamt }
343 1.4.2.2 yamt
344 1.4.2.2 yamt temp_tick = (buf[0] * 256 + buf[1]) & 0x3fff;
345 1.4.2.2 yamt humidity_tick = (buf[2] * 256 + buf[3]) & 0x0fff;
346 1.4.2.2 yamt
347 1.4.2.2 yamt temp = uthum_sht1x_temp(temp_tick);
348 1.4.2.2 yamt rh = uthum_sht1x_rh(humidity_tick, temp);
349 1.4.2.2 yamt
350 1.4.2.2 yamt sc->sc_sensor[UTHUM_HUMIDITY].value_cur = rh / 1000;
351 1.4.2.2 yamt sc->sc_sensor[UTHUM_HUMIDITY].state = ENVSYS_SVALID;
352 1.4.2.2 yamt break;
353 1.4.2.2 yamt case UTHUM_TYPE_TEMPER:
354 1.4.2.2 yamt if (uthum_read_data(sc, CMD_GETTEMP, buf, sizeof(buf), 0) != 0) {
355 1.4.2.2 yamt DPRINTF(("uthum: data read fail\n"));
356 1.4.2.2 yamt sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID;
357 1.4.2.2 yamt return;
358 1.4.2.2 yamt }
359 1.4.2.2 yamt temp_tick = (buf[0] * 256 + buf[1]) & 0xffff;
360 1.4.2.2 yamt temp = uthum_sht1x_temp(temp_tick);
361 1.4.2.2 yamt break;
362 1.4.2.2 yamt default:
363 1.4.2.2 yamt /* do nothing */
364 1.4.2.2 yamt return;
365 1.4.2.2 yamt }
366 1.4.2.2 yamt
367 1.4.2.2 yamt sc->sc_sensor[UTHUM_TEMP].value_cur = (temp * 10000) + 273150000;
368 1.4.2.2 yamt sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SVALID;
369 1.4.2.2 yamt }
370 1.4.2.2 yamt
371 1.4.2.2 yamt /* return C-degree * 100 value */
372 1.4.2.2 yamt int
373 1.4.2.2 yamt uthum_sht1x_temp(unsigned int ticks)
374 1.4.2.2 yamt {
375 1.4.2.2 yamt return (ticks - 4010);
376 1.4.2.2 yamt }
377 1.4.2.2 yamt
378 1.4.2.2 yamt /* return %RH * 1000 */
379 1.4.2.2 yamt int
380 1.4.2.2 yamt uthum_sht1x_rh(unsigned int ticks, int temp)
381 1.4.2.2 yamt {
382 1.4.2.2 yamt int rh_l, rh;
383 1.4.2.2 yamt
384 1.4.2.2 yamt rh_l = (-40000 + 405 * ticks) - ((7 * ticks * ticks) / 250);
385 1.4.2.2 yamt rh = ((temp - 2500) * (1 + (ticks >> 7)) + rh_l) / 10;
386 1.4.2.2 yamt return rh;
387 1.4.2.2 yamt }
388