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