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