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