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