xref: /freebsd/sys/dev/usb/misc/ugold.c (revision 069ac184)
1 /*	$OpenBSD: ugold.c,v 1.7 2014/12/11 18:39:27 mpi Exp $   */
2 
3 /*
4  * Copyright (c) 2013 Takayoshi SASANO <sasano@openbsd.org>
5  * Copyright (c) 2013 Martin Pieuchot <mpi@openbsd.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 Microdia's HID based TEMPer Temperature sensor */
21 
22 #include <sys/cdefs.h>
23 #include <sys/stdint.h>
24 #include <sys/stddef.h>
25 #include <sys/param.h>
26 #include <sys/queue.h>
27 #include <sys/types.h>
28 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/bus.h>
31 #include <sys/module.h>
32 #include <sys/lock.h>
33 #include <sys/mutex.h>
34 #include <sys/condvar.h>
35 #include <sys/sysctl.h>
36 #include <sys/sx.h>
37 #include <sys/unistd.h>
38 #include <sys/callout.h>
39 #include <sys/malloc.h>
40 #include <sys/priv.h>
41 #include <sys/conf.h>
42 
43 #include <dev/hid/hid.h>
44 
45 #include <dev/usb/usb.h>
46 #include <dev/usb/usbdi.h>
47 #include <dev/usb/usbhid.h>
48 #include <dev/usb/usb_process.h>
49 #include <dev/usb/usbdi_util.h>
50 #include "usbdevs.h"
51 
52 #define	USB_DEBUG_VAR usb_debug
53 #include <dev/usb/usb_debug.h>
54 
55 #define	UGOLD_INNER		0
56 #define	UGOLD_OUTER		1
57 #define	UGOLD_MAX_SENSORS	2
58 
59 #define	UGOLD_CMD_DATA		0x80
60 #define	UGOLD_CMD_INIT		0x82
61 
62 enum {
63 	UGOLD_INTR_DT,
64 	UGOLD_N_TRANSFER,
65 };
66 
67 /*
68  * This driver only uses two of the three known commands for the
69  * TEMPerV1.2 device.
70  *
71  * The first byte of the answer corresponds to the command and the
72  * second one seems to be the size (in bytes) of the answer.
73  *
74  * The device always sends 8 bytes and if the length of the answer
75  * is less than that, it just leaves the last bytes untouched.  That
76  * is why most of the time the last n bytes of the answers are the
77  * same.
78  *
79  * The third command below seems to generate two answers with a
80  * string corresponding to the device, for example:
81  *	'TEMPer1F' and '1.1Per1F' (here Per1F is repeated).
82  */
83 static uint8_t cmd_data[8] = {0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00};
84 static uint8_t cmd_init[8] = {0x01, 0x82, 0x77, 0x01, 0x00, 0x00, 0x00, 0x00};
85 
86 #if 0
87 static uint8_t cmd_type[8] = {0x01, 0x86, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00};
88 
89 #endif
90 
91 struct ugold_softc;
92 struct ugold_readout_msg {
93 	struct usb_proc_msg hdr;
94 	struct ugold_softc *sc;
95 };
96 
97 struct ugold_softc {
98 	struct usb_device *sc_udev;
99 	struct usb_xfer *sc_xfer[UGOLD_N_TRANSFER];
100 
101 	struct callout sc_callout;
102 	struct mtx sc_mtx;
103 	struct ugold_readout_msg sc_readout_msg[2];
104 
105 	int	sc_num_sensors;
106 	int	sc_sensor[UGOLD_MAX_SENSORS];
107   	int	sc_calib[UGOLD_MAX_SENSORS];
108 	int	sc_valid[UGOLD_MAX_SENSORS];
109 	uint8_t	sc_report_id;
110 	uint8_t	sc_iface_index[2];
111 };
112 
113 /* prototypes */
114 
115 static device_probe_t ugold_probe;
116 static device_attach_t ugold_attach;
117 static device_detach_t ugold_detach;
118 
119 static usb_proc_callback_t ugold_readout_msg;
120 
121 static usb_callback_t ugold_intr_callback;
122 
123 static device_method_t ugold_methods[] = {
124 	DEVMETHOD(device_probe, ugold_probe),
125 	DEVMETHOD(device_attach, ugold_attach),
126 	DEVMETHOD(device_detach, ugold_detach),
127 
128 	DEVMETHOD_END
129 };
130 
131 static driver_t ugold_driver = {
132 	.name = "ugold",
133 	.methods = ugold_methods,
134 	.size = sizeof(struct ugold_softc),
135 };
136 
137 static const STRUCT_USB_HOST_ID ugold_devs[] = {
138 	{USB_VPI(USB_VENDOR_CHICONY2, USB_PRODUCT_CHICONY2_TEMPER, 0)},
139 };
140 
141 DRIVER_MODULE(ugold, uhub, ugold_driver, NULL, NULL);
142 MODULE_DEPEND(ugold, usb, 1, 1, 1);
143 MODULE_DEPEND(ugold, hid, 1, 1, 1);
144 MODULE_VERSION(ugold, 1);
145 USB_PNP_HOST_INFO(ugold_devs);
146 
147 static const struct usb_config ugold_config[UGOLD_N_TRANSFER] = {
148 	[UGOLD_INTR_DT] = {
149 		.type = UE_INTERRUPT,
150 		.endpoint = UE_ADDR_ANY,
151 		.direction = UE_DIR_IN,
152 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
153 		.bufsize = 0,		/* use wMaxPacketSize */
154 		.callback = &ugold_intr_callback,
155 		.if_index = 1,
156 	},
157 };
158 
159 static void
160 ugold_timeout(void *arg)
161 {
162 	struct ugold_softc *sc = arg;
163 
164 	usb_proc_explore_lock(sc->sc_udev);
165 	(void)usb_proc_explore_msignal(sc->sc_udev,
166 	    &sc->sc_readout_msg[0], &sc->sc_readout_msg[1]);
167 	usb_proc_explore_unlock(sc->sc_udev);
168 
169 	callout_reset(&sc->sc_callout, 6 * hz, &ugold_timeout, sc);
170 }
171 
172 static int
173 ugold_probe(device_t dev)
174 {
175 	struct usb_attach_arg *uaa;
176 
177 	uaa = device_get_ivars(dev);
178 	if (uaa->usb_mode != USB_MODE_HOST)
179 		return (ENXIO);
180 	if (uaa->info.bInterfaceClass != UICLASS_HID)
181 		return (ENXIO);
182 	if (uaa->info.bIfaceIndex != 0)
183 		return (ENXIO);
184 
185 	return (usbd_lookup_id_by_uaa(ugold_devs, sizeof(ugold_devs), uaa));
186 }
187 
188 static int
189 ugold_attach(device_t dev)
190 {
191 	struct ugold_softc *sc = device_get_softc(dev);
192 	struct usb_attach_arg *uaa = device_get_ivars(dev);
193 	struct sysctl_oid *sensor_tree;
194 	uint16_t d_len;
195 	void *d_ptr;
196 	int error;
197 	int i;
198 
199 	sc->sc_udev = uaa->device;
200 	sc->sc_readout_msg[0].hdr.pm_callback = &ugold_readout_msg;
201 	sc->sc_readout_msg[0].sc = sc;
202 	sc->sc_readout_msg[1].hdr.pm_callback = &ugold_readout_msg;
203 	sc->sc_readout_msg[1].sc = sc;
204 	sc->sc_iface_index[0] = uaa->info.bIfaceIndex;
205 	sc->sc_iface_index[1] = uaa->info.bIfaceIndex + 1;
206 
207 	device_set_usb_desc(dev);
208 	mtx_init(&sc->sc_mtx, "ugold lock", NULL, MTX_DEF | MTX_RECURSE);
209 	callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
210 
211 	/* grab all interfaces from other drivers */
212 	for (i = 0;; i++) {
213 		if (i == uaa->info.bIfaceIndex)
214 			continue;
215 		if (usbd_get_iface(uaa->device, i) == NULL)
216 			break;
217 
218 		usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
219 	}
220 
221 	/* figure out report ID */
222 	error = usbd_req_get_hid_desc(uaa->device, NULL,
223 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
224 
225 	if (error)
226 		goto detach;
227 
228 	(void)hid_report_size_max(d_ptr, d_len, hid_input, &sc->sc_report_id);
229 
230 	free(d_ptr, M_TEMP);
231 
232 	error = usbd_transfer_setup(uaa->device,
233 	    sc->sc_iface_index, sc->sc_xfer, ugold_config,
234 	    UGOLD_N_TRANSFER, sc, &sc->sc_mtx);
235 	if (error)
236 		goto detach;
237 
238 	sensor_tree = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev),
239 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensors",
240 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
241 
242 	if (sensor_tree == NULL) {
243 		error = ENOMEM;
244 		goto detach;
245 	}
246 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
247 	    SYSCTL_CHILDREN(sensor_tree),
248 	    OID_AUTO, "inner", CTLFLAG_RD, &sc->sc_sensor[UGOLD_INNER], 0,
249 	    "Inner temperature in microCelsius");
250 
251 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
252 	    SYSCTL_CHILDREN(sensor_tree),
253 	    OID_AUTO, "inner_valid", CTLFLAG_RD, &sc->sc_valid[UGOLD_INNER], 0,
254 	    "Inner temperature is valid");
255 
256 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
257 	    SYSCTL_CHILDREN(sensor_tree),
258 	    OID_AUTO, "inner_calib", CTLFLAG_RWTUN, &sc->sc_calib[UGOLD_INNER], 0,
259 	    "Inner calibration temperature in microCelsius");
260 
261 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
262 	    SYSCTL_CHILDREN(sensor_tree),
263 	    OID_AUTO, "outer", CTLFLAG_RD, &sc->sc_sensor[UGOLD_OUTER], 0,
264 	    "Outer temperature in microCelsius");
265 
266 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
267 	    SYSCTL_CHILDREN(sensor_tree),
268 	    OID_AUTO, "outer_calib", CTLFLAG_RWTUN, &sc->sc_calib[UGOLD_OUTER], 0,
269 	    "Outer calibration temperature in microCelsius");
270 
271 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
272 	    SYSCTL_CHILDREN(sensor_tree),
273 	    OID_AUTO, "outer_valid", CTLFLAG_RD, &sc->sc_valid[UGOLD_OUTER], 0,
274 	    "Outer temperature is valid");
275 
276 	mtx_lock(&sc->sc_mtx);
277 	usbd_transfer_start(sc->sc_xfer[UGOLD_INTR_DT]);
278 	ugold_timeout(sc);
279 	mtx_unlock(&sc->sc_mtx);
280 
281 	return (0);
282 
283 detach:
284 	DPRINTF("error=%s\n", usbd_errstr(error));
285 	ugold_detach(dev);
286 	return (error);
287 }
288 
289 static int
290 ugold_detach(device_t dev)
291 {
292 	struct ugold_softc *sc = device_get_softc(dev);
293 
294 	callout_drain(&sc->sc_callout);
295 
296 	usb_proc_explore_lock(sc->sc_udev);
297 	usb_proc_explore_mwait(sc->sc_udev,
298 	    &sc->sc_readout_msg[0], &sc->sc_readout_msg[1]);
299 	usb_proc_explore_unlock(sc->sc_udev);
300 
301 	usbd_transfer_unsetup(sc->sc_xfer, UGOLD_N_TRANSFER);
302 
303 	mtx_destroy(&sc->sc_mtx);
304 
305 	return (0);
306 }
307 
308 static int
309 ugold_ds75_temp(uint8_t msb, uint8_t lsb)
310 {
311 	/* DS75: 12bit precision mode : 0.0625 degrees Celsius ticks */
312 	/* NOTE: MSB has a sign bit for negative temperatures */
313 	int32_t temp = (msb << 24) | ((lsb & 0xF0) << 16);
314 	return (((int64_t)temp * (int64_t)1000000LL) >> 24);
315 }
316 
317 static void
318 ugold_intr_callback(struct usb_xfer *xfer, usb_error_t error)
319 {
320 	struct ugold_softc *sc = usbd_xfer_softc(xfer);
321 	struct usb_page_cache *pc;
322 	uint8_t buf[8];
323 	int temp;
324 	int len;
325 
326 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
327 
328 	switch (USB_GET_STATE(xfer)) {
329 	case USB_ST_TRANSFERRED:
330 		memset(buf, 0, sizeof(buf));
331 
332 		pc = usbd_xfer_get_frame(xfer, 0);
333 		usbd_copy_out(pc, 0, buf, MIN(len, sizeof(buf)));
334 
335 		switch (buf[0]) {
336 		case UGOLD_CMD_INIT:
337 			if (sc->sc_num_sensors)
338 				break;
339 
340 			sc->sc_num_sensors = MIN(buf[1], UGOLD_MAX_SENSORS) /* XXX */ ;
341 
342 			DPRINTF("%d sensor%s type ds75/12bit (temperature)\n",
343 			    sc->sc_num_sensors, (sc->sc_num_sensors == 1) ? "" : "s");
344 			break;
345 		case UGOLD_CMD_DATA:
346 			switch (buf[1]) {
347 			case 4:
348 				temp = ugold_ds75_temp(buf[4], buf[5]);
349 				sc->sc_sensor[UGOLD_OUTER] = temp + sc->sc_calib[UGOLD_OUTER];
350 				sc->sc_valid[UGOLD_OUTER] = 1;
351 				/* FALLTHROUGH */
352 			case 2:
353 				temp = ugold_ds75_temp(buf[2], buf[3]);
354 				sc->sc_sensor[UGOLD_INNER] = temp + sc->sc_calib[UGOLD_INNER];
355 				sc->sc_valid[UGOLD_INNER] = 1;
356 				break;
357 			default:
358 				DPRINTF("invalid data length (%d bytes)\n", buf[1]);
359 			}
360 			break;
361 		default:
362 			DPRINTF("unknown command 0x%02x\n", buf[0]);
363 			break;
364 		}
365 		/* FALLTHROUGH */
366 	case USB_ST_SETUP:
367 tr_setup:
368 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
369 		usbd_transfer_submit(xfer);
370 		break;
371 	default:			/* Error */
372 		if (error != USB_ERR_CANCELLED) {
373 			/* try clear stall first */
374 			usbd_xfer_set_stall(xfer);
375 			goto tr_setup;
376 		}
377 		break;
378 	}
379 }
380 
381 static int
382 ugold_issue_cmd(struct ugold_softc *sc, uint8_t *cmd, int len)
383 {
384 	return (usbd_req_set_report(sc->sc_udev, &sc->sc_mtx, cmd, len,
385 	    sc->sc_iface_index[1], UHID_OUTPUT_REPORT, sc->sc_report_id));
386 }
387 
388 static void
389 ugold_readout_msg(struct usb_proc_msg *pm)
390 {
391 	struct ugold_softc *sc = ((struct ugold_readout_msg *)pm)->sc;
392 
393 	usb_proc_explore_unlock(sc->sc_udev);
394 
395 	mtx_lock(&sc->sc_mtx);
396 	if (sc->sc_num_sensors == 0)
397 		ugold_issue_cmd(sc, cmd_init, sizeof(cmd_init));
398 
399 	ugold_issue_cmd(sc, cmd_data, sizeof(cmd_data));
400 	mtx_unlock(&sc->sc_mtx);
401 
402 	usb_proc_explore_lock(sc->sc_udev);
403 }
404