Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*	$NetBSD: uthum.c,v 1.23 2022/03/28 12:44:17 riastradh Exp $   */
/*	$OpenBSD: uthum.c,v 1.6 2010/01/03 18:43:02 deraadt Exp $   */

/*
 * Copyright (c) 2009 Yojiro UO <yuo@nui.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * Driver for TEMPer and TEMPerHUM HID
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uthum.c,v 1.23 2022/03/28 12:44:17 riastradh Exp $");

#ifdef _KERNEL_OPT
#include "opt_usb.h"
#endif

#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/conf.h>

#include <dev/sysmon/sysmonvar.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/uhidev.h>
#include <dev/hid/hid.h>

#ifdef UTHUM_DEBUG
int	uthumdebug = 0;
#define DPRINTFN(n, x)	do { if (uthumdebug > (n)) printf x; } while (0)
#else
#define DPRINTFN(n, x)
#endif

#define DPRINTF(x) DPRINTFN(0, x)


/* TEMPerHUM */
#define CMD_DEVTYPE 0x52 /* XXX */
#define CMD_GETDATA 0x48 /* XXX */
#define CMD_GETTEMP 0x54 /* XXX */
static const uint8_t cmd_start[8] =
	{ 0x0a, 0x0b, 0x0c, 0x0d, 0x00, 0x00, 0x02, 0x00 };
static const uint8_t cmd_end[8] =
	{ 0x0a, 0x0b, 0x0c, 0x0d, 0x00, 0x00, 0x01, 0x00 };

/* sensors */
#define UTHUM_TEMP		0
#define UTHUM_HUMIDITY		1
#define UTHUM_MAX_SENSORS	2

#define UTHUM_TYPE_UNKNOWN	0
#define UTHUM_TYPE_SHT1x	1
#define UTHUM_TYPE_TEMPER	2

struct uthum_softc {
	device_t		 sc_dev;
	struct uhidev *		 sc_hdev;
	struct usbd_device *	 sc_udev;
	u_char			 sc_dying;
	uint16_t		 sc_flag;
	int			 sc_sensortype;

	/* uhidev parameters */
	size_t			 sc_flen;	/* feature report length */
	size_t			 sc_olen;	/* output report length */

	/* sensor framework */
	struct sysmon_envsys		 *sc_sme;
	envsys_data_t			 sc_sensor[UTHUM_MAX_SENSORS];

	uint8_t			 sc_num_sensors;
};


static const struct usb_devno uthum_devs[] = {
	/* XXX: various TEMPer variants using same VID/PID */
	{ USB_VENDOR_TENX, USB_PRODUCT_TENX_TEMPER},
};
#define uthum_lookup(v, p) usb_lookup(uthum_devs, v, p)

static int uthum_match(device_t, cfdata_t, void *);
static void uthum_attach(device_t, device_t, void *);
static int uthum_detach(device_t, int);
static int uthum_activate(device_t, enum devact);

static int uthum_read_data(struct uthum_softc *, uint8_t, uint8_t *, size_t, int);
static int uthum_check_sensortype(struct uthum_softc *);
static int uthum_temper_temp(uint8_t, uint8_t);
static int uthum_sht1x_temp(uint8_t, uint8_t);
static int uthum_sht1x_rh(unsigned int, int);

static void uthum_refresh(struct sysmon_envsys *, envsys_data_t *);


CFATTACH_DECL_NEW(uthum, sizeof(struct uthum_softc), uthum_match, uthum_attach,
    uthum_detach, uthum_activate);

static int
uthum_match(device_t parent, cfdata_t match, void *aux)
{
	struct uhidev_attach_arg *uha = aux;

	return uthum_lookup(uha->uiaa->uiaa_vendor, uha->uiaa->uiaa_product)
	    != NULL ? UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
}

static void
uthum_attach(device_t parent, device_t self, void *aux)
{
	struct uthum_softc *sc = device_private(self);
	struct uhidev_attach_arg *uha = aux;
	struct usbd_device *dev = uha->uiaa->uiaa_device;
	int size, repid;
	void *desc;

	sc->sc_dev = self;
	sc->sc_hdev = uha->parent;
	sc->sc_udev = dev;
	sc->sc_num_sensors = 0;

	aprint_normal("\n");
	aprint_naive("\n");

	uhidev_get_report_desc(uha->parent, &desc, &size);
	repid = uha->reportid;
	sc->sc_olen = hid_report_size(desc, size, hid_output, repid);
	sc->sc_flen = hid_report_size(desc, size, hid_feature, repid);

	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);

	if (sc->sc_flen < 32) {
		/* not sensor interface, just attach */
		return;
	}

	sc->sc_sensortype = uthum_check_sensortype(sc);

	/* attach sensor */
	sc->sc_sme = sysmon_envsys_create();
	sc->sc_sme->sme_name = device_xname(self);

	switch (sc->sc_sensortype) {
	case UTHUM_TYPE_SHT1x:
		(void)strlcpy(sc->sc_sensor[UTHUM_TEMP].desc, "temp",
		    sizeof(sc->sc_sensor[UTHUM_TEMP].desc));
		sc->sc_sensor[UTHUM_TEMP].units = ENVSYS_STEMP;
		sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID;

		(void)strlcpy(sc->sc_sensor[UTHUM_HUMIDITY].desc,
		    "relative humidity",
		    sizeof(sc->sc_sensor[UTHUM_HUMIDITY].desc));
		sc->sc_sensor[UTHUM_HUMIDITY].units = ENVSYS_INTEGER;
		sc->sc_sensor[UTHUM_HUMIDITY].value_cur = 0;
		sc->sc_sensor[UTHUM_HUMIDITY].state = ENVSYS_SINVALID;

		sysmon_envsys_sensor_attach(sc->sc_sme,
		    &sc->sc_sensor[UTHUM_TEMP]);
		sysmon_envsys_sensor_attach(sc->sc_sme,
		    &sc->sc_sensor[UTHUM_HUMIDITY]);
		sc->sc_num_sensors = 2;
		DPRINTF(("sensor type: SHT1x\n"));
		break;
	case UTHUM_TYPE_TEMPER:
		(void)strlcpy(sc->sc_sensor[UTHUM_TEMP].desc, "temp",
		    sizeof(sc->sc_sensor[UTHUM_TEMP].desc));
		sc->sc_sensor[UTHUM_TEMP].units = ENVSYS_STEMP;
		sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID;

		sysmon_envsys_sensor_attach(sc->sc_sme,
		    &sc->sc_sensor[UTHUM_TEMP]);
		sc->sc_num_sensors = 1;
		DPRINTF(("sensor type: TEMPer\n"));
		break;
	case UTHUM_TYPE_UNKNOWN:
		DPRINTF(("sensor type: unknown, give up to attach sensors\n"));
	default:
		break;
	}

	if (sc->sc_num_sensors > 0) {
		sc->sc_sme->sme_cookie = sc;
		sc->sc_sme->sme_refresh = uthum_refresh;

		if (sysmon_envsys_register(sc->sc_sme)) {
			sysmon_envsys_destroy(sc->sc_sme);
			sc->sc_sme = NULL;
			aprint_error_dev(self,
			    "unable to register with sysmon\n");
		}
	} else {
		sysmon_envsys_destroy(sc->sc_sme);
		sc->sc_sme = NULL;
	}

	DPRINTF(("uthum_attach: complete\n"));
}

static int
uthum_detach(device_t self, int flags)
{
	struct uthum_softc *sc = device_private(self);
	int rv = 0;

	sc->sc_dying = 1;

	if (sc->sc_sme != NULL)
		sysmon_envsys_unregister(sc->sc_sme);

	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);

	return rv;
}

static int
uthum_activate(device_t self, enum devact act)
{
	struct uthum_softc *sc = device_private(self);

	switch (act) {
	case DVACT_DEACTIVATE:
		sc->sc_dying = 1;
		break;
	}
	return 0;
}

static int
uthum_read_data(struct uthum_softc *sc, uint8_t target_cmd, uint8_t *buf,
	size_t len, int need_delay)
{
	int i;
	uint8_t cmdbuf[32], report[256];
	size_t olen, flen;

	/* if return buffer is null, do nothing */
	if ((buf == NULL) || len == 0)
		return 0;

	olen = uimin(sc->sc_olen, sizeof(cmdbuf));

	/* issue query */
	memset(cmdbuf, 0, sizeof(cmdbuf));
	memcpy(cmdbuf, cmd_start, sizeof(cmd_start));
	if (uhidev_set_report(sc->sc_hdev, UHID_OUTPUT_REPORT,
	    cmdbuf, olen))
		return EIO;

	memset(cmdbuf, 0, sizeof(cmdbuf));
	cmdbuf[0] = target_cmd;
	if (uhidev_set_report(sc->sc_hdev, UHID_OUTPUT_REPORT,
	    cmdbuf, olen))
		return EIO;

	memset(cmdbuf, 0, sizeof(cmdbuf));
	for (i = 0; i < 7; i++) {
		if (uhidev_set_report(sc->sc_hdev, UHID_OUTPUT_REPORT,
		    cmdbuf, olen))
			return EIO;
	}
	memcpy(cmdbuf, cmd_end, sizeof(cmd_end));
	if (uhidev_set_report(sc->sc_hdev, UHID_OUTPUT_REPORT,
	    cmdbuf, olen))
		return EIO;

	/* wait if required */
	if (need_delay > 1)
		kpause("uthum", false, (need_delay*hz+999)/1000 + 1, NULL);

	/* get answer */
	flen = uimin(sc->sc_flen, sizeof(report));
	if (uhidev_get_report(sc->sc_hdev, UHID_FEATURE_REPORT,
	    report, flen))
		return EIO;
	memcpy(buf, report, len);
	return 0;
}

static int
uthum_check_sensortype(struct uthum_softc *sc)
{
	uint8_t buf[8];
	static uint8_t sht1x_sig0[] =
	    { 0x57, 0x5a, 0x13, 0x00, 0x14, 0x00, 0x53, 0x00 };
	static uint8_t sht1x_sig1[] =
	    { 0x57, 0x5a, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 };
	static uint8_t temper_sig[] =
	    { 0x57, 0x58, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 };

	if (uthum_read_data(sc, CMD_DEVTYPE, buf, sizeof(buf), 0) != 0) {
		DPRINTF(("uthum: read fail\n"));
		return UTHUM_TYPE_UNKNOWN;
	}

	/*
	 * currently we have not enough information about the return value,
	 * therefore, compare full bytes.
	 * TEMPerHUM HID (SHT1x version) will return:
	 *   { 0x57, 0x5a, 0x13, 0x00, 0x14, 0x00, 0x53, 0x00 }
	 *   { 0x57, 0x5a, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 }
	 * TEMPer HID (TEMPer version) will return:
	 *   { 0x57, 0x58, 0x14, 0x00, 0x14, 0x00, 0x53, 0x00 }
	 */
	DPRINTF(("uthum: device signature: "
	    "0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x\n",
	    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]));
	if (0 == memcmp(buf, sht1x_sig0, sizeof(sht1x_sig0)))
		return UTHUM_TYPE_SHT1x;
	if (0 == memcmp(buf, sht1x_sig1, sizeof(sht1x_sig1)))
		return UTHUM_TYPE_SHT1x;
	if (0 == memcmp(buf, temper_sig, sizeof(temper_sig)))
		return UTHUM_TYPE_TEMPER;

	return UTHUM_TYPE_UNKNOWN;
}


static void
uthum_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
{
	struct uthum_softc *sc = sme->sme_cookie;
	uint8_t buf[8];
	unsigned int humidity_tick;
	int temp, rh;

	switch (sc->sc_sensortype) {
	case UTHUM_TYPE_SHT1x:
		if (uthum_read_data(sc, CMD_GETDATA, buf, sizeof(buf), 1000)
		    != 0) {
			DPRINTF(("uthum: data read fail\n"));
			sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID;
			sc->sc_sensor[UTHUM_HUMIDITY].state = ENVSYS_SINVALID;
			return;
		}
		DPRINTF(("%s: read SHT1x data "
		    "0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x\n",
		    sc->sc_sme->sme_name, buf[0], buf[1], buf[2], buf[3],
		    buf[4], buf[5], buf[6], buf[7]));

		humidity_tick = (buf[2] * 256 + buf[3]) & 0x0fff;

		temp = uthum_sht1x_temp(buf[0], buf[1]);
		rh = uthum_sht1x_rh(humidity_tick, temp);

		sc->sc_sensor[UTHUM_HUMIDITY].value_cur = rh / 1000;
		sc->sc_sensor[UTHUM_HUMIDITY].state = ENVSYS_SVALID;
		break;
	case UTHUM_TYPE_TEMPER:
		if (uthum_read_data(sc, CMD_GETTEMP, buf, sizeof(buf), 0) != 0) {
			DPRINTF(("uthum: data read fail\n"));
			sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SINVALID;
			return;
		}
		DPRINTF(("%s: read TEMPER data "
		    "0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x\n",
		    sc->sc_sme->sme_name, buf[0], buf[1], buf[2], buf[3],
		    buf[4], buf[5], buf[6], buf[7]));
		temp = uthum_temper_temp(buf[0], buf[1]);
		break;
	default:
		/* do nothing */
		return;
	}

	sc->sc_sensor[UTHUM_TEMP].value_cur = (temp * 10000) + 273150000;
	sc->sc_sensor[UTHUM_TEMP].state = ENVSYS_SVALID;
}

/* return C-degree * 100 value */
static int
uthum_temper_temp(uint8_t msb, uint8_t lsb)
{
	int val;

	val = (msb << 8) | lsb;
	if (val >= 32768) {
		val = val - 65536;
	}
	val = (val * 100) >> 8;
	return val;
}

/* return C-degree * 100 value */
static int
uthum_sht1x_temp(uint8_t msb, uint8_t lsb)
{
	int val;

	val = ((msb << 8) + lsb) - 4096;
	return val;
}

/* return %RH * 1000 */
static int
uthum_sht1x_rh(unsigned int ticks, int temp)
{
	int rh_l, rh;

	rh_l = (-40000 + 405 * ticks) - ((7 * ticks * ticks) / 250);
	rh = ((temp - 2500) * (1 + (ticks >> 7)) + rh_l) / 10;
	return rh;
}