xref: /openbsd/sys/dev/usb/usps.c (revision 905646f0)
1 /*	$OpenBSD: usps.c,v 1.10 2020/07/31 10:49:33 mglocker Exp $   */
2 
3 /*
4  * Copyright (c) 2011 Yojiro UO <yuo@nui.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /* Driver for usb smart power strip FX-5204PS */
20 
21 #include <sys/param.h>
22 #include <sys/systm.h>
23 #include <sys/kernel.h>
24 #include <sys/malloc.h>
25 #include <sys/device.h>
26 #include <sys/conf.h>
27 #include <sys/sensors.h>
28 
29 #include <dev/usb/usb.h>
30 #include <dev/usb/usbdi.h>
31 #include <dev/usb/usbdi_util.h>
32 #include <dev/usb/usbdevs.h>
33 
34 #ifdef USPS_DEBUG
35 int	uspsdebug = 0;
36 #define DPRINTFN(n, x)	do { if (uspsdebug > (n)) printf x; } while (0)
37 #else
38 #define DPRINTFN(n, x)
39 #endif
40 
41 #define DPRINTF(x) DPRINTFN(0, x)
42 
43 #define USPS_UPDATE_TICK	1 /* sec */
44 #define USPS_TIMEOUT		1000 /* ms */
45 #define USPS_INTR_TICKS		50 /* ms */
46 
47 /* protocol */
48 #define USPS_CMD_START		0x01
49 #define USPS_CMD_VALUE		0x20
50 #define USPS_CMD_GET_FIRMWARE	0xc0
51 #define USPS_CMD_GET_SERIAL	0xc1
52 #define USPS_CMD_GET_VOLTAGE	0xb0
53 #define USPS_CMD_GET_TEMP	0xb4
54 #define USPS_CMD_GET_FREQ	0xa1
55 #define USPS_CMD_GET_UNK0	0xa2
56 
57 #define USPS_MODE_WATTAGE	0x10
58 #define USPS_MODE_CURRENT	0x30
59 
60 #define FX5204_NUM_PORTS	4
61 
62 struct usps_port_sensor {
63 	struct ksensor ave;
64 	struct ksensor min;
65 	struct ksensor max;
66 	int vave, vmin, vmax;
67 };
68 
69 struct usps_softc {
70 	struct device		 sc_dev;
71 	struct usbd_device	*sc_udev;
72 	struct usbd_interface	*sc_iface;
73 	struct usbd_pipe	*sc_ipipe;
74 	int			 sc_isize;
75 	struct usbd_xfer	*sc_xfer;
76 	uint8_t			 sc_buf[16];
77 	uint8_t			 *sc_intrbuf;
78 
79 	uint16_t		 sc_flag;
80 
81 	/* device info */
82 	uint8_t		 	 sc_firmware_version[2];
83 	uint32_t		 sc_device_serial;
84 
85 	/* sensor framework */
86 	struct usps_port_sensor	 sc_port_sensor[FX5204_NUM_PORTS];
87 	struct usps_port_sensor	 sc_total_sensor;
88 	struct ksensor 		 sc_voltage_sensor;
89 	struct ksensor		 sc_frequency_sensor;
90 	struct ksensor		 sc_temp_sensor;
91 	struct ksensor		 sc_serial_sensor;
92 	struct ksensordev	 sc_sensordev;
93 	struct sensor_task	*sc_sensortask;
94 
95 	int			 sc_count;
96 };
97 
98 struct usps_port_pkt {
99 	uint8_t		header; /* should be 0x80 */
100 	uint16_t	seq;
101 	uint8_t		padding[5];
102 	uint16_t	port[4];
103 } __packed; /* 16 byte length struct */
104 
105 static const struct usb_devno usps_devs[] = {
106 	{ USB_VENDOR_FUJITSUCOMP, USB_PRODUCT_FUJITSUCOMP_FX5204PS},
107 };
108 #define usps_lookup(v, p) usb_lookup(usps_devs, v, p)
109 
110 int  usps_match(struct device *, void *, void *);
111 void usps_attach(struct device *, struct device *, void *);
112 int  usps_detach(struct device *, int);
113 void usps_intr(struct usbd_xfer *, void *, usbd_status);
114 
115 usbd_status usps_cmd(struct usps_softc *, uint8_t, uint16_t, uint16_t);
116 usbd_status usps_set_measurement_mode(struct usps_softc *, int);
117 
118 void usps_get_device_info(struct usps_softc *);
119 void usps_refresh(void *);
120 void usps_refresh_temp(struct usps_softc *);
121 void usps_refresh_power(struct usps_softc *);
122 void usps_refresh_ports(struct usps_softc *);
123 
124 struct cfdriver usps_cd = {
125 	NULL, "usps", DV_DULL
126 };
127 
128 const struct cfattach usps_ca = {
129 	sizeof(struct usps_softc), usps_match, usps_attach, usps_detach
130 };
131 
132 int
133 usps_match(struct device *parent, void *match, void *aux)
134 {
135 	struct usb_attach_arg *uaa = aux;
136 
137 	if (uaa->iface == NULL || uaa->configno != 1)
138 		return UMATCH_NONE;
139 
140 	if (usps_lookup(uaa->vendor, uaa->product) == NULL)
141 		return UMATCH_NONE;
142 
143 	return (UMATCH_VENDOR_PRODUCT);
144 }
145 
146 void
147 usps_attach(struct device *parent, struct device *self, void *aux)
148 {
149 	struct usps_softc *sc = (struct usps_softc *)self;
150 	struct usb_attach_arg *uaa = aux;
151 	usb_interface_descriptor_t *id;
152 	usb_endpoint_descriptor_t *ed;
153 	int ep_ibulk, ep_obulk, ep_intr;
154 	usbd_status err;
155 	int i;
156 
157 	sc->sc_udev = uaa->device;
158 
159 #define USPS_USB_IFACE 0
160 
161 	/* get interface handle */
162 	if ((err = usbd_device2interface_handle(sc->sc_udev, USPS_USB_IFACE,
163 		&sc->sc_iface)) != 0) {
164 		printf("%s: failed to get interface %d: %s\n",
165 		    sc->sc_dev.dv_xname, USPS_USB_IFACE, usbd_errstr(err));
166 		return;
167 	}
168 
169 	/* find endpoints */
170 	ep_ibulk = ep_obulk = ep_intr = -1;
171 	id = usbd_get_interface_descriptor(sc->sc_iface);
172 	for (i = 0; i < id->bNumEndpoints; i++) {
173 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
174 		if (ed == NULL) {
175 			printf("%s: failed to get endpoint %d descriptor\n",
176 			    sc->sc_dev.dv_xname, i);
177 			return;
178 		}
179 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
180 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
181 			ep_ibulk = ed->bEndpointAddress;
182 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
183 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
184 			ep_obulk = ed->bEndpointAddress;
185 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
186 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT){
187 			ep_intr = ed->bEndpointAddress;
188 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
189 		}
190 	}
191 
192 	if (ep_intr == -1) {
193 		printf("%s: no data endpoint found\n", sc->sc_dev.dv_xname);
194 		return;
195 	}
196 
197 	usps_get_device_info(sc);
198 	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
199 	    sizeof(sc->sc_sensordev.xname));
200 
201 	/* attach sensor */
202 	sc->sc_voltage_sensor.type = SENSOR_VOLTS_AC;
203 	sc->sc_frequency_sensor.type = SENSOR_FREQ;
204 	sc->sc_temp_sensor.type = SENSOR_TEMP;
205 	sc->sc_serial_sensor.type = SENSOR_INTEGER;
206 	sensor_attach(&sc->sc_sensordev, &sc->sc_voltage_sensor);
207 	sensor_attach(&sc->sc_sensordev, &sc->sc_frequency_sensor);
208 	sensor_attach(&sc->sc_sensordev, &sc->sc_temp_sensor);
209 	sensor_attach(&sc->sc_sensordev, &sc->sc_serial_sensor);
210 
211 	sc->sc_serial_sensor.value = sc->sc_device_serial;
212 	strlcpy(sc->sc_serial_sensor.desc, "unit serial#",
213 	    sizeof(sc->sc_serial_sensor.desc));
214 
215 	/*
216 	 * XXX: the device has mode of par port sensor, Watt of Ampair.
217 	 * currently only watt mode is selected.
218 	 */
219 	usps_set_measurement_mode(sc, USPS_MODE_WATTAGE);
220 	for (i = 0; i < FX5204_NUM_PORTS; i++) {
221 		sc->sc_port_sensor[i].ave.type = SENSOR_WATTS;
222 		sc->sc_port_sensor[i].min.type = SENSOR_WATTS;
223 		sc->sc_port_sensor[i].max.type = SENSOR_WATTS;
224 		sensor_attach(&sc->sc_sensordev, &sc->sc_port_sensor[i].ave);
225 		sensor_attach(&sc->sc_sensordev, &sc->sc_port_sensor[i].min);
226 		sensor_attach(&sc->sc_sensordev, &sc->sc_port_sensor[i].max);
227 		(void)snprintf(sc->sc_port_sensor[i].ave.desc,
228 		    sizeof(sc->sc_port_sensor[i].ave.desc),
229 		    "port#%d (average)", i);
230 		(void)snprintf(sc->sc_port_sensor[i].min.desc,
231 		    sizeof(sc->sc_port_sensor[i].min.desc),
232 		    "port#%d (min)", i);
233 		(void)snprintf(sc->sc_port_sensor[i].max.desc,
234 		    sizeof(sc->sc_port_sensor[i].max.desc),
235 		    "port#%d (max)", i);
236 	}
237 
238 	sc->sc_total_sensor.ave.type = SENSOR_WATTS;
239 	sc->sc_total_sensor.min.type = SENSOR_WATTS;
240 	sc->sc_total_sensor.max.type = SENSOR_WATTS;
241 	sensor_attach(&sc->sc_sensordev, &sc->sc_total_sensor.ave);
242 	sensor_attach(&sc->sc_sensordev, &sc->sc_total_sensor.min);
243 	sensor_attach(&sc->sc_sensordev, &sc->sc_total_sensor.max);
244 	(void)snprintf(sc->sc_total_sensor.ave.desc,
245 	    sizeof(sc->sc_total_sensor.ave.desc), "total (average)", i);
246 	(void)snprintf(sc->sc_total_sensor.min.desc,
247 	    sizeof(sc->sc_total_sensor.ave.desc), "total (min)", i);
248 	(void)snprintf(sc->sc_total_sensor.max.desc,
249 	    sizeof(sc->sc_total_sensor.ave.desc), "total (max)", i);
250 
251 	sc->sc_sensortask = sensor_task_register(sc, usps_refresh,
252 	    USPS_UPDATE_TICK);
253 	if (sc->sc_sensortask == NULL) {
254 		printf(", unable to register update task\n");
255 		goto fail;
256 	}
257 
258 	printf("%s: device#=%d, firmware version=V%02dL%02d\n",
259 	    sc->sc_dev.dv_xname, sc->sc_device_serial,
260 	    sc->sc_firmware_version[0],
261 	    sc->sc_firmware_version[1]);
262 
263 	sensordev_install(&sc->sc_sensordev);
264 
265 	/* open interrupt endpoint */
266 	sc->sc_intrbuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
267 	if (sc->sc_intrbuf == NULL)
268 		goto fail;
269 	err = usbd_open_pipe_intr(sc->sc_iface, ep_intr,
270 	    USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_intrbuf,
271 	    sc->sc_isize, usps_intr, USPS_INTR_TICKS);
272 	if (err) {
273 		printf("%s: could not open intr pipe %s\n",
274 		    sc->sc_dev.dv_xname, usbd_errstr(err));
275 		goto fail;
276 	}
277 
278 	DPRINTF(("usps_attach: complete\n"));
279 	return;
280 
281 fail:
282 	if (sc->sc_ipipe != NULL)
283 		usbd_close_pipe(sc->sc_ipipe);
284 	if (sc->sc_xfer != NULL)
285 		usbd_free_xfer(sc->sc_xfer);
286 	if (sc->sc_intrbuf != NULL)
287 		free(sc->sc_intrbuf, M_USBDEV, sc->sc_isize);
288 }
289 
290 int
291 usps_detach(struct device *self, int flags)
292 {
293 	struct usps_softc *sc = (struct usps_softc *)self;
294 	int i, rv = 0, s;
295 
296 	usbd_deactivate(sc->sc_udev);
297 
298 	s = splusb();
299 	if (sc->sc_ipipe != NULL) {
300 		usbd_close_pipe(sc->sc_ipipe);
301 		if (sc->sc_intrbuf != NULL)
302 			free(sc->sc_intrbuf, M_USBDEV, sc->sc_isize);
303 		sc->sc_ipipe = NULL;
304 	}
305 	if (sc->sc_xfer != NULL)
306 		usbd_free_xfer(sc->sc_xfer);
307 	splx(s);
308 
309 	wakeup(&sc->sc_sensortask);
310 	sensordev_deinstall(&sc->sc_sensordev);
311 	sensor_detach(&sc->sc_sensordev, &sc->sc_voltage_sensor);
312 	sensor_detach(&sc->sc_sensordev, &sc->sc_frequency_sensor);
313 	sensor_detach(&sc->sc_sensordev, &sc->sc_temp_sensor);
314 	sensor_detach(&sc->sc_sensordev, &sc->sc_serial_sensor);
315 	for (i = 0; i < FX5204_NUM_PORTS; i++) {
316 		sensor_detach(&sc->sc_sensordev, &sc->sc_port_sensor[i].ave);
317 		sensor_detach(&sc->sc_sensordev, &sc->sc_port_sensor[i].min);
318 		sensor_detach(&sc->sc_sensordev, &sc->sc_port_sensor[i].max);
319 	}
320 	sensor_detach(&sc->sc_sensordev, &sc->sc_total_sensor.ave);
321 	sensor_detach(&sc->sc_sensordev, &sc->sc_total_sensor.min);
322 	sensor_detach(&sc->sc_sensordev, &sc->sc_total_sensor.max);
323 
324 	if (sc->sc_sensortask != NULL)
325 		sensor_task_unregister(sc->sc_sensortask);
326 
327 	return (rv);
328 }
329 
330 usbd_status
331 usps_cmd(struct usps_softc *sc, uint8_t cmd, uint16_t val, uint16_t len)
332 {
333 	usb_device_request_t req;
334 	usbd_status err;
335 
336 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
337 	req.bRequest = cmd;
338 	USETW(req.wValue, val);
339 	USETW(req.wIndex, 0);
340 	USETW(req.wLength, len);
341 
342 	err = usbd_do_request(sc->sc_udev, &req, &sc->sc_buf);
343 	if (err) {
344 		printf("%s: could not issue sensor cmd: %s\n",
345 		    sc->sc_dev.dv_xname, usbd_errstr(err));
346 		return (EIO);
347 	}
348 
349 	return (0);
350 }
351 
352 usbd_status
353 usps_set_measurement_mode(struct usps_softc *sc, int mode)
354 {
355 	usb_device_request_t req;
356 	usbd_status err;
357 
358 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
359 	req.bRequest = USPS_CMD_START;
360 	USETW(req.wValue, 0);
361 	USETW(req.wIndex, 0);
362 	USETW(req.wLength, 0);
363 
364 	err = usbd_do_request(sc->sc_udev, &req, &sc->sc_buf);
365 	if (err) {
366 		printf("%s: fail to set sensor mode: %s\n",
367 		    sc->sc_dev.dv_xname, usbd_errstr(err));
368 		return (EIO);
369 	}
370 
371 	req.bRequest = USPS_CMD_VALUE;
372 	USETW(req.wValue, mode);
373 
374 	err = usbd_do_request(sc->sc_udev, &req, &sc->sc_buf);
375 	if (err) {
376 		printf("%s: could not set sensor mode: %s\n",
377 		    sc->sc_dev.dv_xname, usbd_errstr(err));
378 		return (EIO);
379 	}
380 
381 	return (0);
382 }
383 
384 void
385 usps_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
386 {
387 	struct usps_softc *sc = priv;
388 	struct usps_port_pkt *pkt;
389 	struct usps_port_sensor *ps;
390 	int i, total;
391 
392 	if (usbd_is_dying(sc->sc_udev))
393 		return;
394 
395 	if (status != USBD_NORMAL_COMPLETION) {
396 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
397 			return;
398 		if (status == USBD_STALLED)
399 			usbd_clear_endpoint_stall_async(sc->sc_ipipe);
400 		return;
401 	}
402 
403 	/* process intr packet */
404 	if (sc->sc_intrbuf == NULL)
405 		return;
406 
407 	pkt = (struct usps_port_pkt *)sc->sc_intrbuf;
408 
409 	total = 0;
410 	for (i = 0; i < FX5204_NUM_PORTS; i++) {
411 		ps = &sc->sc_port_sensor[i];
412 		if (sc->sc_count == 0)
413 			ps->vmax = ps->vmin = pkt->port[i];
414 		if (pkt->port[i] > ps->vmax)
415 			ps->vmax = pkt->port[i];
416 		if (pkt->port[i] < ps->vmin)
417 			ps->vmin = pkt->port[i];
418 		ps->vave =
419 		    (ps->vave * sc->sc_count + pkt->port[i])/(sc->sc_count +1);
420 		total += pkt->port[i];
421 	}
422 
423 	/* calculate ports total */
424 	ps = &sc->sc_total_sensor;
425 	if (sc->sc_count == 0)
426 		ps->vmax = ps->vmin = total;
427 	if (total > ps->vmax)
428 		ps->vmax = total;
429 	if (total < ps->vmin)
430 		ps->vmin = total;
431 	ps->vave = (ps->vave * sc->sc_count + total)/(sc->sc_count +1);
432 
433 	sc->sc_count++;
434 }
435 
436 void
437 usps_get_device_info(struct usps_softc *sc)
438 {
439 	int serial;
440 
441 	/* get Firmware version */
442 	usps_cmd(sc, USPS_CMD_GET_FIRMWARE, 0, 2);
443 	sc->sc_firmware_version[0] =
444 	    (sc->sc_buf[0]>>4) * 10 + (sc->sc_buf[0] & 0xf);
445 	sc->sc_firmware_version[1] =
446 	    (sc->sc_buf[1]>>4) * 10 + (sc->sc_buf[1] & 0xf);
447 
448 	/* get device serial number */
449 	usps_cmd(sc, USPS_CMD_GET_SERIAL, 0, 3);
450 
451 	serial = 0;
452 	serial += ((sc->sc_buf[0]>>4) * 10 + (sc->sc_buf[0] & 0xf)) * 10000;
453 	serial += ((sc->sc_buf[1]>>4) * 10 + (sc->sc_buf[1] & 0xf)) * 100;
454 	serial += ((sc->sc_buf[2]>>4) * 10 + (sc->sc_buf[2] & 0xf));
455 	sc->sc_device_serial = serial;
456 }
457 
458 void
459 usps_refresh(void *arg)
460 {
461 	struct usps_softc *sc = arg;
462 
463 	usps_refresh_temp(sc);
464 	usps_refresh_power(sc);
465 	usps_refresh_ports(sc);
466 }
467 
468 void
469 usps_refresh_ports(struct usps_softc *sc)
470 {
471 	int i;
472 	struct usps_port_sensor *ps;
473 
474 	/* update port values */
475 	for (i = 0; i < FX5204_NUM_PORTS; i++) {
476 		ps = &sc->sc_port_sensor[i];
477 		ps->ave.value = ps->vave * 1000000;
478 		ps->min.value = ps->vmin * 1000000;
479 		ps->max.value = ps->vmax * 1000000;
480 	}
481 
482 	/* update total value */
483 	ps = &sc->sc_total_sensor;
484 	ps->ave.value = ps->vave * 1000000;
485 	ps->min.value = ps->vmin * 1000000;
486 	ps->max.value = ps->vmax * 1000000;
487 
488 	sc->sc_count = 0;
489 }
490 
491 void
492 usps_refresh_temp(struct usps_softc *sc)
493 {
494 	int temp;
495 
496 	if (usps_cmd(sc, USPS_CMD_GET_TEMP, 0, 2) != 0) {
497 		DPRINTF(("%s: temperature data read error\n",
498 		    sc->sc_dev.dv_xname));
499 		sc->sc_temp_sensor.flags |= SENSOR_FINVALID;
500 		return;
501 	}
502 	temp = (sc->sc_buf[1] << 8) + sc->sc_buf[0];
503 	sc->sc_temp_sensor.value = (temp * 10000) + 273150000;
504 	sc->sc_temp_sensor.flags &= ~SENSOR_FINVALID;
505 }
506 
507 void
508 usps_refresh_power(struct usps_softc *sc)
509 {
510 	int v;
511 	uint val;
512 	uint64_t f;
513 
514 	/* update source voltage */
515 	if (usps_cmd(sc, USPS_CMD_GET_VOLTAGE, 0, 1) != 0) {
516 		DPRINTF(("%s: voltage data read error\n", sc->sc_dev.dv_xname));
517 		sc->sc_voltage_sensor.flags |= SENSOR_FINVALID;
518 		return;
519 	}
520 
521 	v = sc->sc_buf[0] * 1000000;
522 	sc->sc_voltage_sensor.value = v;
523 	sc->sc_voltage_sensor.flags &= ~SENSOR_FINVALID;
524 
525 	/* update source frequency */
526 	if (usps_cmd(sc, USPS_CMD_GET_FREQ, 0, 8) != 0) {
527 		DPRINTF(("%s: frequency data read error\n",
528 		    sc->sc_dev.dv_xname));
529 		sc->sc_frequency_sensor.flags |= SENSOR_FINVALID;
530 		return;
531 	}
532 
533 	if (sc->sc_buf[7] == 0 && sc->sc_buf[6] == 0) {
534 		/* special case */
535 		f = 0;
536 	} else {
537 		val = (sc->sc_buf[1] << 8) + sc->sc_buf[0];
538 		if (val == 0) {
539 			/* guard against "division by zero" */
540 			sc->sc_frequency_sensor.flags |= SENSOR_FINVALID;
541 			return;
542 		}
543 		f = 2000000L;
544 		f *=  1000000L;
545 		f /= val;
546 	}
547 
548 	sc->sc_frequency_sensor.value = f;
549 	sc->sc_frequency_sensor.flags &= ~SENSOR_FINVALID;
550 }
551