xref: /openbsd/sys/dev/usb/utwitch.c (revision 81508fe3)
1 /*	$OpenBSD: utwitch.c,v 1.23 2024/05/23 03:21:09 jsg Exp $ */
2 
3 /*
4  * Copyright (c) 2010 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 Maywa-Denki & KAYAC YUREX BBU sensor */
20 /* this driver was previously known as uyurex(4). */
21 
22 #include <sys/param.h>
23 #include <sys/systm.h>
24 #include <sys/malloc.h>
25 #include <sys/device.h>
26 #include <sys/sensors.h>
27 
28 #include <dev/usb/usb.h>
29 #include <dev/usb/usbhid.h>
30 #include <dev/usb/usbdi.h>
31 #include <dev/usb/usbdevs.h>
32 #include <dev/usb/uhidev.h>
33 
34 #define	CMD_NONE	0xf0
35 #define CMD_EOF		0x0d
36 #define CMD_ACK		0x21
37 #define	CMD_MODE	0x41 /* XXX */
38 #define	CMD_VALUE	0x43
39 #define CMD_READ	0x52
40 #define CMD_WRITE	0x53
41 #define CMD_PADDING	0xff
42 
43 #define UPDATE_TICK	5 /* sec */
44 
45 #ifdef UYUREX_DEBUG
46 #define DPRINTF(x)	do { printf x; } while (0)
47 #else
48 #define DPRINTF(x)
49 #endif
50 
51 struct utwitch_softc {
52 	struct uhidev		 sc_hdev;
53 	struct usbd_device	*sc_udev;
54 
55 	/* uhidev parameters */
56 	size_t			 sc_ilen;	/* input report length */
57 	size_t			 sc_olen;	/* output report length */
58 
59 	uint8_t			*sc_ibuf;
60 
61 	/* sensor framework */
62 	struct ksensor		 sc_sensor_val;
63 	struct ksensor		 sc_sensor_delta;
64 	struct ksensordev	 sc_sensordev;
65 	struct sensor_task	*sc_sensortask;
66 
67 	/* device private */
68 	int			 sc_initialized;
69 	uint8_t			 issueing_cmd;
70 	uint8_t			 accepted_cmd;
71 
72 	uint32_t		 sc_curval;
73 	uint32_t		 sc_oldval;
74 };
75 
76 const struct usb_devno utwitch_devs[] = {
77 	{ USB_VENDOR_MICRODIA, USB_PRODUCT_MICRODIA_YUREX},
78 };
79 
80 int utwitch_match(struct device *, void *, void *);
81 void utwitch_attach(struct device *, struct device *, void *);
82 int utwitch_detach(struct device *, int);
83 
84 void utwitch_set_mode(struct utwitch_softc *, uint8_t);
85 void utwitch_read_value_request(struct utwitch_softc *);
86 void utwitch_write_value_request(struct utwitch_softc *, uint32_t);
87 
88 void utwitch_intr(struct uhidev *, void *, u_int);
89 void utwitch_refresh(void *);
90 
91 struct cfdriver utwitch_cd = {
92 	NULL, "utwitch", DV_DULL
93 };
94 
95 const struct cfattach utwitch_ca = {
96 	sizeof(struct utwitch_softc),
97 	utwitch_match,
98 	utwitch_attach,
99 	utwitch_detach
100 };
101 
102 int
utwitch_match(struct device * parent,void * match,void * aux)103 utwitch_match(struct device *parent, void *match, void *aux)
104 {
105 	struct uhidev_attach_arg *uha = aux;
106 
107 	if (UHIDEV_CLAIM_MULTIPLE_REPORTID(uha))
108 		return (UMATCH_NONE);
109 
110 	return (usb_lookup(utwitch_devs, uha->uaa->vendor, uha->uaa->product) != NULL ?
111 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
112 }
113 
114 void
utwitch_attach(struct device * parent,struct device * self,void * aux)115 utwitch_attach(struct device *parent, struct device *self, void *aux)
116 {
117 	struct utwitch_softc *sc = (struct utwitch_softc *)self;
118 	struct usb_attach_arg *uaa = aux;
119 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa;
120 	struct usbd_device *dev = uha->parent->sc_udev;
121 	int size, repid, err;
122 	void *desc;
123 
124 	sc->sc_udev = dev;
125 	sc->sc_hdev.sc_intr = utwitch_intr;
126 	sc->sc_hdev.sc_parent = uha->parent;
127 	sc->sc_hdev.sc_report_id = uha->reportid;
128 
129 	uhidev_get_report_desc(uha->parent, &desc, &size);
130 	repid = uha->reportid;
131 	sc->sc_ilen = hid_report_size(desc, size, hid_input, repid);
132 	sc->sc_olen = hid_report_size(desc, size, hid_output, repid);
133 
134 	err = uhidev_open(&sc->sc_hdev);
135 	if (err) {
136 		printf("%s: uhidev_open %d\n", __func__, err);
137 		return;
138 	}
139 	sc->sc_ibuf = malloc(sc->sc_ilen, M_USBDEV, M_WAITOK);
140 
141 	printf("\n");
142 
143 
144 	/* attach sensor */
145 	strlcpy(sc->sc_sensordev.xname, sc->sc_hdev.sc_dev.dv_xname,
146 	    sizeof(sc->sc_sensordev.xname));
147 
148 	/* add BBU sensor */
149 	sc->sc_sensor_val.type = SENSOR_INTEGER;
150 	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor_val);
151 	strlcpy(sc->sc_sensor_val.desc, "BBU",
152 		sizeof(sc->sc_sensor_val.desc));
153 
154 	/* add BBU delta sensor */
155 	sc->sc_sensor_delta.type = SENSOR_INTEGER;
156 	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor_delta);
157 	strlcpy(sc->sc_sensor_delta.desc, "mBBU/sec",
158 		sizeof(sc->sc_sensor_delta.desc));
159 
160 	sc->sc_sensortask = sensor_task_register(sc, utwitch_refresh, UPDATE_TICK);
161 	if (sc->sc_sensortask == NULL) {
162 		printf(", unable to register update task\n");
163 		return;
164 	}
165 	sensordev_install(&sc->sc_sensordev);
166 
167 	DPRINTF(("utwitch_attach: complete\n"));
168 
169 	/* init device */ /* XXX */
170 	utwitch_set_mode(sc, 0);
171 }
172 
173 int
utwitch_detach(struct device * self,int flags)174 utwitch_detach(struct device *self, int flags)
175 {
176 	struct utwitch_softc *sc = (struct utwitch_softc *)self;
177 	int rv = 0;
178 
179 	wakeup(&sc->sc_sensortask);
180 	sensordev_deinstall(&sc->sc_sensordev);
181 	sensor_detach(&sc->sc_sensordev, &sc->sc_sensor_val);
182 	sensor_detach(&sc->sc_sensordev, &sc->sc_sensor_delta);
183 	if (sc->sc_sensortask != NULL)
184 		sensor_task_unregister(sc->sc_sensortask);
185 
186 	if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
187 		uhidev_close(&sc->sc_hdev);
188 
189 	if (sc->sc_ibuf != NULL) {
190 		free(sc->sc_ibuf, M_USBDEV, sc->sc_ilen);
191 		sc->sc_ibuf = NULL;
192 	}
193 
194 	return (rv);
195 }
196 
197 void
utwitch_intr(struct uhidev * addr,void * ibuf,u_int len)198 utwitch_intr(struct uhidev *addr, void *ibuf, u_int len)
199 {
200 	struct utwitch_softc *sc = (struct utwitch_softc *)addr;
201 	uint8_t buf[8];
202 	uint32_t val;
203 
204 	if (sc->sc_ibuf == NULL)
205 		return;
206 
207 	/* process requests */
208 	memcpy(buf, ibuf, 8);
209 	DPRINTF(("intr: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n",
210 		buf[0], buf[1], buf[2], buf[3],
211 		buf[4], buf[5], buf[6], buf[7]));
212 
213 
214 	switch (buf[0]) {
215 	case CMD_ACK:
216 		if (buf[1] == sc->issueing_cmd) {
217 			DPRINTF(("ack received for cmd 0x%.2x\n", buf[1]));
218 			sc->accepted_cmd = buf[1];
219 		} else {
220 			DPRINTF(("cmd-ack mismatch: recved 0x%.2x, expect 0x%.2x\n",
221 				buf[1], sc->issueing_cmd));
222 			/* discard previous command */
223 			sc->accepted_cmd = CMD_NONE;
224 			sc->issueing_cmd = CMD_NONE;
225 		}
226 		break;
227 	case CMD_READ:
228 	case CMD_VALUE:
229 		val = (buf[2] << 24) + (buf[3] << 16) + (buf[4] << 8)  + buf[5];
230 		if (!sc->sc_initialized) {
231 			sc->sc_oldval = val;
232 			sc->sc_initialized = 1;
233 		}
234 		sc->sc_sensor_val.value = val;
235 		sc->sc_curval = val;
236 		DPRINTF(("recv value update message: %d\n", val));
237 		break;
238 	default:
239 		DPRINTF(("unknown message: 0x%.2x\n", buf[0]));
240 	}
241 
242 	return;
243 }
244 
245 void
utwitch_refresh(void * arg)246 utwitch_refresh(void *arg)
247 {
248 	struct utwitch_softc *sc = arg;
249 
250 	if (!sc->sc_initialized) {
251 		utwitch_read_value_request(sc);
252 	} else {
253 		/* calculate delta value */
254 		sc->sc_sensor_delta.value =
255 			(1000 * (sc->sc_curval - sc->sc_oldval)) / UPDATE_TICK;
256 		sc->sc_oldval = sc->sc_curval;
257 	}
258 }
259 
260 void
utwitch_set_mode(struct utwitch_softc * sc,uint8_t val)261 utwitch_set_mode(struct utwitch_softc *sc, uint8_t val)
262 {
263 	uint8_t req[8];
264 	int olen;
265 
266 	olen = MIN(sc->sc_olen, sizeof(req));
267 	memset(req, CMD_PADDING, sizeof(req));
268 	req[0] = CMD_MODE;
269 	req[1] = val;
270 	req[2] = CMD_EOF;
271 	if (uhidev_set_report(sc->sc_hdev.sc_parent, UHID_OUTPUT_REPORT,
272 	    sc->sc_hdev.sc_report_id, req, olen) != olen) {
273 		printf("uhidev_set_report error:EIO\n");
274 		return;
275 	}
276 
277 	/* wait ack */
278 	tsleep_nsec(&sc->sc_sensortask, 0, "utwitch", MSEC_TO_NSEC(1000));
279 }
280 
281 void
utwitch_read_value_request(struct utwitch_softc * sc)282 utwitch_read_value_request(struct utwitch_softc *sc)
283 {
284 	uint8_t req[8];
285 	int olen;
286 
287 	olen = MIN(sc->sc_olen, sizeof(req));
288 	memset(req, CMD_PADDING, sizeof(req));
289 	req[0] = CMD_READ;
290 	req[1] = CMD_EOF;
291 	sc->issueing_cmd = CMD_READ;
292 	sc->accepted_cmd = CMD_NONE;
293 	if (uhidev_set_report(sc->sc_hdev.sc_parent, UHID_OUTPUT_REPORT,
294 	    sc->sc_hdev.sc_report_id, req, olen) != olen)
295 		return;
296 
297 	/* wait till sensor data are updated, 500ms will be enough */
298 	tsleep_nsec(&sc->sc_sensortask, 0, "utwitch", MSEC_TO_NSEC(500));
299 }
300 
301 void
utwitch_write_value_request(struct utwitch_softc * sc,uint32_t val)302 utwitch_write_value_request(struct utwitch_softc *sc, uint32_t val)
303 {
304 	uint32_t v;
305 	uint8_t req[8];
306 	int olen;
307 
308 	olen = MIN(sc->sc_olen, sizeof(req));
309 	req[0] = CMD_WRITE;
310 	req[1] = 0;
311 	req[6] = CMD_EOF;
312 	req[7] = CMD_PADDING;
313 	v = htobe32(val);
314 	memcpy(req + 2, &v, sizeof(uint32_t));
315 
316 	sc->issueing_cmd = CMD_WRITE;
317 	sc->accepted_cmd = CMD_NONE;
318 	if (uhidev_set_report(sc->sc_hdev.sc_parent, UHID_OUTPUT_REPORT,
319 	    sc->sc_hdev.sc_report_id, req, olen) != olen)
320 		return;
321 
322 	/* wait till sensor data are updated, 250ms will be enough */
323 	tsleep_nsec(&sc->sc_sensortask, 0, "utwitch", MSEC_TO_NSEC(250));
324 }
325