xref: /openbsd/sys/dev/usb/usps.c (revision 6f40fd34)
1 /*	$OpenBSD: usps.c,v 1.9 2017/04/08 02:57:25 deraadt 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_abort_pipe(sc->sc_ipipe);
301 		usbd_close_pipe(sc->sc_ipipe);
302 		if (sc->sc_intrbuf != NULL)
303 			free(sc->sc_intrbuf, M_USBDEV, sc->sc_isize);
304 		sc->sc_ipipe = NULL;
305 	}
306 	if (sc->sc_xfer != NULL)
307 		usbd_free_xfer(sc->sc_xfer);
308 	splx(s);
309 
310 	wakeup(&sc->sc_sensortask);
311 	sensordev_deinstall(&sc->sc_sensordev);
312 	sensor_detach(&sc->sc_sensordev, &sc->sc_voltage_sensor);
313 	sensor_detach(&sc->sc_sensordev, &sc->sc_frequency_sensor);
314 	sensor_detach(&sc->sc_sensordev, &sc->sc_temp_sensor);
315 	sensor_detach(&sc->sc_sensordev, &sc->sc_serial_sensor);
316 	for (i = 0; i < FX5204_NUM_PORTS; i++) {
317 		sensor_detach(&sc->sc_sensordev, &sc->sc_port_sensor[i].ave);
318 		sensor_detach(&sc->sc_sensordev, &sc->sc_port_sensor[i].min);
319 		sensor_detach(&sc->sc_sensordev, &sc->sc_port_sensor[i].max);
320 	}
321 	sensor_detach(&sc->sc_sensordev, &sc->sc_total_sensor.ave);
322 	sensor_detach(&sc->sc_sensordev, &sc->sc_total_sensor.min);
323 	sensor_detach(&sc->sc_sensordev, &sc->sc_total_sensor.max);
324 
325 	if (sc->sc_sensortask != NULL)
326 		sensor_task_unregister(sc->sc_sensortask);
327 
328 	return (rv);
329 }
330 
331 usbd_status
332 usps_cmd(struct usps_softc *sc, uint8_t cmd, uint16_t val, uint16_t len)
333 {
334 	usb_device_request_t req;
335 	usbd_status err;
336 
337 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
338 	req.bRequest = cmd;
339 	USETW(req.wValue, val);
340 	USETW(req.wIndex, 0);
341 	USETW(req.wLength, len);
342 
343 	err = usbd_do_request(sc->sc_udev, &req, &sc->sc_buf);
344 	if (err) {
345 		printf("%s: could not issue sensor cmd: %s\n",
346 		    sc->sc_dev.dv_xname, usbd_errstr(err));
347 		return (EIO);
348 	}
349 
350 	return (0);
351 }
352 
353 usbd_status
354 usps_set_measurement_mode(struct usps_softc *sc, int mode)
355 {
356 	usb_device_request_t req;
357 	usbd_status err;
358 
359 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
360 	req.bRequest = USPS_CMD_START;
361 	USETW(req.wValue, 0);
362 	USETW(req.wIndex, 0);
363 	USETW(req.wLength, 0);
364 
365 	err = usbd_do_request(sc->sc_udev, &req, &sc->sc_buf);
366 	if (err) {
367 		printf("%s: fail to set sensor mode: %s\n",
368 		    sc->sc_dev.dv_xname, usbd_errstr(err));
369 		return (EIO);
370 	}
371 
372 	req.bRequest = USPS_CMD_VALUE;
373 	USETW(req.wValue, mode);
374 
375 	err = usbd_do_request(sc->sc_udev, &req, &sc->sc_buf);
376 	if (err) {
377 		printf("%s: could not set sensor mode: %s\n",
378 		    sc->sc_dev.dv_xname, usbd_errstr(err));
379 		return (EIO);
380 	}
381 
382 	return (0);
383 }
384 
385 void
386 usps_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
387 {
388 	struct usps_softc *sc = priv;
389 	struct usps_port_pkt *pkt;
390 	struct usps_port_sensor *ps;
391 	int i, total;
392 
393 	if (usbd_is_dying(sc->sc_udev))
394 		return;
395 
396 	if (status != USBD_NORMAL_COMPLETION) {
397 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
398 			return;
399 		if (status == USBD_STALLED)
400 			usbd_clear_endpoint_stall_async(sc->sc_ipipe);
401 		return;
402 	}
403 
404 	/* process intr packet */
405 	if (sc->sc_intrbuf == NULL)
406 		return;
407 
408 	pkt = (struct usps_port_pkt *)sc->sc_intrbuf;
409 
410 	total = 0;
411 	for (i = 0; i < FX5204_NUM_PORTS; i++) {
412 		ps = &sc->sc_port_sensor[i];
413 		if (sc->sc_count == 0)
414 			ps->vmax = ps->vmin = pkt->port[i];
415 		if (pkt->port[i] > ps->vmax)
416 			ps->vmax = pkt->port[i];
417 		if (pkt->port[i] < ps->vmin)
418 			ps->vmin = pkt->port[i];
419 		ps->vave =
420 		    (ps->vave * sc->sc_count + pkt->port[i])/(sc->sc_count +1);
421 		total += pkt->port[i];
422 	}
423 
424 	/* calculate ports total */
425 	ps = &sc->sc_total_sensor;
426 	if (sc->sc_count == 0)
427 		ps->vmax = ps->vmin = total;
428 	if (total > ps->vmax)
429 		ps->vmax = total;
430 	if (total < ps->vmin)
431 		ps->vmin = total;
432 	ps->vave = (ps->vave * sc->sc_count + total)/(sc->sc_count +1);
433 
434 	sc->sc_count++;
435 }
436 
437 void
438 usps_get_device_info(struct usps_softc *sc)
439 {
440 	int serial;
441 
442 	/* get Firmware version */
443 	usps_cmd(sc, USPS_CMD_GET_FIRMWARE, 0, 2);
444 	sc->sc_firmware_version[0] =
445 	    (sc->sc_buf[0]>>4) * 10 + (sc->sc_buf[0] & 0xf);
446 	sc->sc_firmware_version[1] =
447 	    (sc->sc_buf[1]>>4) * 10 + (sc->sc_buf[1] & 0xf);
448 
449 	/* get device serial number */
450 	usps_cmd(sc, USPS_CMD_GET_SERIAL, 0, 3);
451 
452 	serial = 0;
453 	serial += ((sc->sc_buf[0]>>4) * 10 + (sc->sc_buf[0] & 0xf)) * 10000;
454 	serial += ((sc->sc_buf[1]>>4) * 10 + (sc->sc_buf[1] & 0xf)) * 100;
455 	serial += ((sc->sc_buf[2]>>4) * 10 + (sc->sc_buf[2] & 0xf));
456 	sc->sc_device_serial = serial;
457 }
458 
459 void
460 usps_refresh(void *arg)
461 {
462 	struct usps_softc *sc = arg;
463 
464 	usps_refresh_temp(sc);
465 	usps_refresh_power(sc);
466 	usps_refresh_ports(sc);
467 }
468 
469 void
470 usps_refresh_ports(struct usps_softc *sc)
471 {
472 	int i;
473 	struct usps_port_sensor *ps;
474 
475 	/* update port values */
476 	for (i = 0; i < FX5204_NUM_PORTS; i++) {
477 		ps = &sc->sc_port_sensor[i];
478 		ps->ave.value = ps->vave * 1000000;
479 		ps->min.value = ps->vmin * 1000000;
480 		ps->max.value = ps->vmax * 1000000;
481 	}
482 
483 	/* update total value */
484 	ps = &sc->sc_total_sensor;
485 	ps->ave.value = ps->vave * 1000000;
486 	ps->min.value = ps->vmin * 1000000;
487 	ps->max.value = ps->vmax * 1000000;
488 
489 	sc->sc_count = 0;
490 }
491 
492 void
493 usps_refresh_temp(struct usps_softc *sc)
494 {
495 	int temp;
496 
497 	if (usps_cmd(sc, USPS_CMD_GET_TEMP, 0, 2) != 0) {
498 		DPRINTF(("%s: temperature data read error\n",
499 		    sc->sc_dev.dv_xname));
500 		sc->sc_temp_sensor.flags |= SENSOR_FINVALID;
501 		return;
502 	}
503 	temp = (sc->sc_buf[1] << 8) + sc->sc_buf[0];
504 	sc->sc_temp_sensor.value = (temp * 10000) + 273150000;
505 	sc->sc_temp_sensor.flags &= ~SENSOR_FINVALID;
506 }
507 
508 void
509 usps_refresh_power(struct usps_softc *sc)
510 {
511 	int v;
512 	uint val;
513 	uint64_t f;
514 
515 	/* update source voltage */
516 	if (usps_cmd(sc, USPS_CMD_GET_VOLTAGE, 0, 1) != 0) {
517 		DPRINTF(("%s: voltage data read error\n", sc->sc_dev.dv_xname));
518 		sc->sc_voltage_sensor.flags |= SENSOR_FINVALID;
519 		return;
520 	}
521 
522 	v = sc->sc_buf[0] * 1000000;
523 	sc->sc_voltage_sensor.value = v;
524 	sc->sc_voltage_sensor.flags &= ~SENSOR_FINVALID;
525 
526 	/* update source frequency */
527 	if (usps_cmd(sc, USPS_CMD_GET_FREQ, 0, 8) != 0) {
528 		DPRINTF(("%s: frequency data read error\n",
529 		    sc->sc_dev.dv_xname));
530 		sc->sc_frequency_sensor.flags |= SENSOR_FINVALID;
531 		return;
532 	}
533 
534 	if (sc->sc_buf[7] == 0 && sc->sc_buf[6] == 0) {
535 		/* special case */
536 		f = 0;
537 	} else {
538 		val = (sc->sc_buf[1] << 8) + sc->sc_buf[0];
539 		if (val == 0) {
540 			/* guard against "division by zero" */
541 			sc->sc_frequency_sensor.flags |= SENSOR_FINVALID;
542 			return;
543 		}
544 		f = 2000000L;
545 		f *=  1000000L;
546 		f /= val;
547 	}
548 
549 	sc->sc_frequency_sensor.value = f;
550 	sc->sc_frequency_sensor.flags &= ~SENSOR_FINVALID;
551 }
552