xref: /openbsd-src/sys/dev/usb/uow.c (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
1 /*	$OpenBSD: uow.c,v 1.37 2020/07/31 10:49:33 mglocker Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.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 DISCLAIMS 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 /*
20  * Maxim/Dallas DS2490 USB 1-Wire adapter driver.
21  */
22 
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/device.h>
26 #include <sys/kernel.h>
27 
28 #include <dev/onewire/onewirereg.h>
29 #include <dev/onewire/onewirevar.h>
30 
31 #include <dev/usb/usb.h>
32 #include <dev/usb/usbdevs.h>
33 #include <dev/usb/usbdi.h>
34 #include <dev/usb/usbdi_util.h>
35 
36 #include <dev/usb/uowreg.h>
37 
38 #define UOW_TIMEOUT	1000	/* ms */
39 
40 struct uow_softc {
41 	struct device		 sc_dev;
42 
43 	struct onewire_bus	 sc_ow_bus;
44 	struct device		*sc_ow_dev;
45 
46 	struct usbd_device	*sc_udev;
47 	struct usbd_interface	*sc_iface;
48 	struct usbd_pipe	*sc_ph_ibulk;
49 	struct usbd_pipe	*sc_ph_obulk;
50 	struct usbd_pipe	*sc_ph_intr;
51 	u_int8_t		 sc_regs[DS2490_NREGS];
52 	struct usbd_xfer	*sc_xfer_in;
53 	struct usbd_xfer	*sc_xfer_out;
54 	u_int8_t		 sc_fifo[DS2490_DATAFIFOSIZE];
55 };
56 
57 int uow_match(struct device *, void *, void *);
58 void uow_attach(struct device *, struct device *, void *);
59 int uow_detach(struct device *, int);
60 int uow_activate(struct device *, int);
61 
62 struct cfdriver uow_cd = {
63 	NULL, "uow", DV_DULL
64 };
65 
66 const struct cfattach uow_ca = {
67 	sizeof(struct uow_softc),
68 	uow_match,
69 	uow_attach,
70 	uow_detach,
71 	uow_activate,
72 };
73 
74 /* List of supported devices */
75 static const struct usb_devno uow_devs[] = {
76 	{ USB_VENDOR_DALLAS,		USB_PRODUCT_DALLAS_USB_FOB_IBUTTON }
77 };
78 
79 int	uow_ow_reset(void *);
80 int	uow_ow_bit(void *, int);
81 int	uow_ow_read_byte(void *);
82 void	uow_ow_write_byte(void *, int);
83 void	uow_ow_read_block(void *, void *, int);
84 void	uow_ow_write_block(void *, const void *, int);
85 void	uow_ow_matchrom(void *, u_int64_t);
86 int	uow_ow_search(void *, u_int64_t *, int, u_int64_t);
87 
88 int	uow_cmd(struct uow_softc *, int, int, int);
89 #define uow_ctlcmd(s, c, p)	uow_cmd((s), DS2490_CONTROL_CMD, (c), (p))
90 #define uow_commcmd(s, c, p)	uow_cmd((s), DS2490_COMM_CMD, (c), (p))
91 #define uow_modecmd(s, c, p)	uow_cmd((s), DS2490_MODE_CMD, (c), (p))
92 
93 void	uow_intr(struct usbd_xfer *, void *, usbd_status);
94 int	uow_read(struct uow_softc *, void *, int);
95 int	uow_write(struct uow_softc *, const void *, int);
96 int	uow_reset(struct uow_softc *);
97 
98 int
99 uow_match(struct device *parent, void *match, void *aux)
100 {
101 	struct usb_attach_arg *uaa = aux;
102 
103 	if (uaa->iface == NULL || uaa->configno != DS2490_USB_CONFIG)
104 		return (UMATCH_NONE);
105 
106 	return ((usb_lookup(uow_devs, uaa->vendor, uaa->product) != NULL) ?
107 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
108 }
109 
110 void
111 uow_attach(struct device *parent, struct device *self, void *aux)
112 {
113 	struct uow_softc *sc = (struct uow_softc *)self;
114 	struct usb_attach_arg *uaa = aux;
115 	usb_interface_descriptor_t *id;
116 	usb_endpoint_descriptor_t *ed;
117 	int ep_ibulk = -1, ep_obulk = -1, ep_intr = -1;
118 	struct onewirebus_attach_args oba;
119 	usbd_status error;
120 	int i;
121 
122 	sc->sc_udev = uaa->device;
123 
124 	/* Get interface handle */
125 	if ((error = usbd_device2interface_handle(sc->sc_udev,
126 	    DS2490_USB_IFACE, &sc->sc_iface)) != 0) {
127 		printf("%s: failed to get iface %d: %s\n",
128 		    sc->sc_dev.dv_xname, DS2490_USB_IFACE,
129 		    usbd_errstr(error));
130 		return;
131 	}
132 
133 	/* Find endpoints */
134 	id = usbd_get_interface_descriptor(sc->sc_iface);
135 	for (i = 0; i < id->bNumEndpoints; i++) {
136 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
137 		if (ed == NULL) {
138 			printf("%s: failed to get endpoint %d descriptor\n",
139 			    sc->sc_dev.dv_xname, i);
140 			return;
141 		}
142 
143 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
144 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
145 			ep_ibulk = ed->bEndpointAddress;
146 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
147 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
148 			ep_obulk = ed->bEndpointAddress;
149 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
150 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT)
151 			ep_intr = ed->bEndpointAddress;
152 	}
153 	if (ep_ibulk == -1 || ep_obulk == -1 || ep_intr == -1) {
154 		printf("%s: missing endpoint: ibulk %d, obulk %d, intr %d\n",
155 		   sc->sc_dev.dv_xname, ep_ibulk, ep_obulk, ep_intr);
156 		return;
157 	}
158 
159 	/* Open pipes */
160 	if ((error = usbd_open_pipe(sc->sc_iface, ep_ibulk, USBD_EXCLUSIVE_USE,
161 	    &sc->sc_ph_ibulk)) != 0) {
162 		printf("%s: failed to open bulk-in pipe: %s\n",
163 		    sc->sc_dev.dv_xname, usbd_errstr(error));
164 		return;
165 	}
166 	if ((error = usbd_open_pipe(sc->sc_iface, ep_obulk, USBD_EXCLUSIVE_USE,
167 	    &sc->sc_ph_obulk)) != 0) {
168 		printf("%s: failed to open bulk-out pipe: %s\n",
169 		    sc->sc_dev.dv_xname, usbd_errstr(error));
170 		goto fail;
171 	}
172 	if ((error = usbd_open_pipe_intr(sc->sc_iface, ep_intr,
173 	    USBD_SHORT_XFER_OK, &sc->sc_ph_intr, sc,
174 	    sc->sc_regs, sizeof(sc->sc_regs), uow_intr,
175 	    USBD_DEFAULT_INTERVAL)) != 0) {
176 		printf("%s: failed to open intr pipe: %s\n",
177 		    sc->sc_dev.dv_xname, usbd_errstr(error));
178 		goto fail;
179 	}
180 
181 	/* Allocate xfers for bulk transfers */
182 	if ((sc->sc_xfer_in = usbd_alloc_xfer(sc->sc_udev)) == NULL) {
183 		printf("%s: failed to alloc bulk-in xfer\n",
184 		    sc->sc_dev.dv_xname);
185 		goto fail;
186 	}
187 	if ((sc->sc_xfer_out = usbd_alloc_xfer(sc->sc_udev)) == NULL) {
188 		printf("%s: failed to alloc bulk-out xfer\n",
189 		    sc->sc_dev.dv_xname);
190 		goto fail;
191 	}
192 
193 	memset(sc->sc_fifo, 0xff, sizeof(sc->sc_fifo));
194 
195 	/* Reset device */
196 	uow_reset(sc);
197 
198 	/* Attach 1-Wire bus */
199 	sc->sc_ow_bus.bus_cookie = sc;
200 	sc->sc_ow_bus.bus_reset = uow_ow_reset;
201 	sc->sc_ow_bus.bus_bit = uow_ow_bit;
202 	sc->sc_ow_bus.bus_read_byte = uow_ow_read_byte;
203 	sc->sc_ow_bus.bus_write_byte = uow_ow_write_byte;
204 	sc->sc_ow_bus.bus_read_block = uow_ow_read_block;
205 	sc->sc_ow_bus.bus_write_block = uow_ow_write_block;
206 	sc->sc_ow_bus.bus_matchrom = uow_ow_matchrom;
207 #if 0
208 	sc->sc_ow_bus.bus_search = uow_ow_search;
209 #endif
210 
211 	bzero(&oba, sizeof(oba));
212 	oba.oba_bus = &sc->sc_ow_bus;
213 	sc->sc_ow_dev = config_found(self, &oba, onewirebus_print);
214 
215 	return;
216 
217 fail:
218 	if (sc->sc_ph_ibulk != NULL) {
219 		usbd_close_pipe(sc->sc_ph_ibulk);
220 		sc->sc_ph_ibulk = NULL;
221 	}
222 	if (sc->sc_ph_obulk != NULL) {
223 		usbd_close_pipe(sc->sc_ph_obulk);
224 		sc->sc_ph_obulk = NULL;
225 	}
226 	if (sc->sc_ph_intr != NULL) {
227 		usbd_close_pipe(sc->sc_ph_intr);
228 		sc->sc_ph_intr = NULL;
229 	}
230 	if (sc->sc_xfer_in != NULL) {
231 		usbd_free_xfer(sc->sc_xfer_in);
232 		sc->sc_xfer_in = NULL;
233 	}
234 	if (sc->sc_xfer_out != NULL) {
235 		usbd_free_xfer(sc->sc_xfer_out);
236 		sc->sc_xfer_out = NULL;
237 	}
238 }
239 
240 int
241 uow_detach(struct device *self, int flags)
242 {
243 	struct uow_softc *sc = (struct uow_softc *)self;
244 	int rv = 0, s;
245 
246 	s = splusb();
247 
248 	if (sc->sc_ph_ibulk != NULL)
249 		usbd_close_pipe(sc->sc_ph_ibulk);
250 	if (sc->sc_ph_obulk != NULL)
251 		usbd_close_pipe(sc->sc_ph_obulk);
252 	if (sc->sc_ph_intr != NULL)
253 		usbd_close_pipe(sc->sc_ph_intr);
254 
255 	if (sc->sc_xfer_in != NULL)
256 		usbd_free_xfer(sc->sc_xfer_in);
257 	if (sc->sc_xfer_out != NULL)
258 		usbd_free_xfer(sc->sc_xfer_out);
259 
260 	if (sc->sc_ow_dev != NULL)
261 		rv = config_detach(sc->sc_ow_dev, flags);
262 
263 	splx(s);
264 
265 	return (rv);
266 }
267 
268 int
269 uow_activate(struct device *self, int act)
270 {
271 	struct uow_softc *sc = (struct uow_softc *)self;
272 	int rv = 0;
273 
274 	switch (act) {
275 	case DVACT_DEACTIVATE:
276 		if (sc->sc_ow_dev != NULL)
277 			rv = config_deactivate(sc->sc_ow_dev);
278 		usbd_deactivate(sc->sc_udev);
279 		break;
280 	}
281 
282 	return (rv);
283 }
284 
285 int
286 uow_ow_reset(void *arg)
287 {
288 	struct uow_softc *sc = arg;
289 
290 	if (uow_commcmd(sc, DS2490_COMM_1WIRE_RESET | DS2490_BIT_IM, 0) != 0)
291 		return (1);
292 
293 	/* XXX: check presence pulse */
294 	return (0);
295 }
296 
297 int
298 uow_ow_bit(void *arg, int value)
299 {
300 	struct uow_softc *sc = arg;
301 	u_int8_t data;
302 
303 	if (uow_commcmd(sc, DS2490_COMM_BIT_IO | DS2490_BIT_IM |
304 	    (value ? DS2490_BIT_D : 0), 0) != 0)
305 		return (1);
306 	if (uow_read(sc, &data, 1) != 1)
307 		return (1);
308 
309 	return (data);
310 }
311 
312 int
313 uow_ow_read_byte(void *arg)
314 {
315 	struct uow_softc *sc = arg;
316 	u_int8_t data;
317 
318 	if (uow_commcmd(sc, DS2490_COMM_BYTE_IO | DS2490_BIT_IM, 0xff) != 0)
319 		return (-1);
320 	if (uow_read(sc, &data, 1) != 1)
321 		return (-1);
322 
323 	return (data);
324 }
325 
326 void
327 uow_ow_write_byte(void *arg, int value)
328 {
329 	struct uow_softc *sc = arg;
330 	u_int8_t data;
331 
332 	if (uow_commcmd(sc, DS2490_COMM_BYTE_IO | DS2490_BIT_IM, value) != 0)
333 		return;
334 	uow_read(sc, &data, sizeof(data));
335 }
336 
337 void
338 uow_ow_read_block(void *arg, void *buf, int len)
339 {
340 	struct uow_softc *sc = arg;
341 
342 	if (uow_write(sc, sc->sc_fifo, len) != 0)
343 		return;
344 	if (uow_commcmd(sc, DS2490_COMM_BLOCK_IO | DS2490_BIT_IM, len) != 0)
345 		return;
346 	uow_read(sc, buf, len);
347 }
348 
349 void
350 uow_ow_write_block(void *arg, const void *buf, int len)
351 {
352 	struct uow_softc *sc = arg;
353 
354 	if (uow_write(sc, buf, len) != 0)
355 		return;
356 	if (uow_commcmd(sc, DS2490_COMM_BLOCK_IO | DS2490_BIT_IM, len) != 0)
357 		return;
358 }
359 
360 void
361 uow_ow_matchrom(void *arg, u_int64_t rom)
362 {
363 	struct uow_softc *sc = arg;
364 	u_int8_t data[8];
365 	int i;
366 
367 	for (i = 0; i < 8; i++)
368 		data[i] = (rom >> (i * 8)) & 0xff;
369 
370 	if (uow_write(sc, data, 8) != 0)
371 		return;
372 	if (uow_commcmd(sc, DS2490_COMM_MATCH_ACCESS | DS2490_BIT_IM,
373 	    ONEWIRE_CMD_MATCH_ROM) != 0)
374 		return;
375 }
376 
377 int
378 uow_ow_search(void *arg, u_int64_t *buf, int size, u_int64_t startrom)
379 {
380 	struct uow_softc *sc = arg;
381 	u_int8_t data[8];
382 	int i, rv;
383 
384 	for (i = 0; i < 8; i++)
385 		data[i] = (startrom >> (i * 8)) & 0xff;
386 
387 	if (uow_write(sc, data, 8) != 0)
388 		return (-1);
389 	if (uow_commcmd(sc, DS2490_COMM_SEARCH_ACCESS | DS2490_BIT_IM |
390 	    DS2490_BIT_SM | DS2490_BIT_RST | DS2490_BIT_F, size << 8 |
391 	    ONEWIRE_CMD_SEARCH_ROM) != 0)
392 		return (-1);
393 
394 	if ((rv = uow_read(sc, buf, size * 8)) == -1)
395 		return (-1);
396 
397 	return (rv / 8);
398 }
399 
400 int
401 uow_cmd(struct uow_softc *sc, int type, int cmd, int param)
402 {
403 	usb_device_request_t req;
404 	usbd_status error;
405 
406 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
407 	req.bRequest = type;
408 	USETW(req.wValue, cmd);
409 	USETW(req.wIndex, param);
410 	USETW(req.wLength, 0);
411 	if ((error = usbd_do_request(sc->sc_udev, &req, NULL)) != 0) {
412 		printf("%s: cmd failed, type 0x%02x, cmd 0x%04x, "
413 		    "param 0x%04x: %s\n", sc->sc_dev.dv_xname, type, cmd,
414 		    param, usbd_errstr(error));
415 		if (cmd != DS2490_CTL_RESET_DEVICE)
416 			uow_reset(sc);
417 		return (1);
418 	}
419 
420 again:
421 	if (tsleep_nsec(sc->sc_regs, PRIBIO, "uowcmd",
422 	    MSEC_TO_NSEC(UOW_TIMEOUT)) != 0) {
423 		printf("%s: cmd timeout, type 0x%02x, cmd 0x%04x, "
424 		    "param 0x%04x\n", sc->sc_dev.dv_xname, type, cmd,
425 		    param);
426 		return (1);
427 	}
428 	if ((sc->sc_regs[DS2490_ST_STFL] & DS2490_ST_STFL_IDLE) == 0)
429 		goto again;
430 
431 	return (0);
432 }
433 
434 void
435 uow_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
436 {
437 	struct uow_softc *sc = priv;
438 
439 	if (status != USBD_NORMAL_COMPLETION) {
440 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
441 			return;
442 		if (status == USBD_STALLED)
443 			usbd_clear_endpoint_stall_async(sc->sc_ph_intr);
444 		return;
445 	}
446 
447 	wakeup(sc->sc_regs);
448 }
449 
450 int
451 uow_read(struct uow_softc *sc, void *buf, int len)
452 {
453 	usbd_status error;
454 	int count;
455 
456 	/* XXX: implement FIFO status monitoring */
457 	if (len > DS2490_DATAFIFOSIZE) {
458 		printf("%s: read %d bytes, xfer too big\n",
459 		    sc->sc_dev.dv_xname, len);
460 		return (-1);
461 	}
462 
463 	usbd_setup_xfer(sc->sc_xfer_in, sc->sc_ph_ibulk, sc, buf, len,
464 	    USBD_SHORT_XFER_OK | USBD_SYNCHRONOUS, UOW_TIMEOUT, NULL);
465 	error = usbd_transfer(sc->sc_xfer_in);
466 	if (error != 0) {
467 		printf("%s: read failed, len %d: %s\n",
468 		    sc->sc_dev.dv_xname, len, usbd_errstr(error));
469 		uow_reset(sc);
470 		return (-1);
471 	}
472 
473 	usbd_get_xfer_status(sc->sc_xfer_in, NULL, NULL, &count, &error);
474 	return (count);
475 }
476 
477 int
478 uow_write(struct uow_softc *sc, const void *buf, int len)
479 {
480 	usbd_status error;
481 
482 	/* XXX: implement FIFO status monitoring */
483 	if (len > DS2490_DATAFIFOSIZE) {
484 		printf("%s: write %d bytes, xfer too big\n",
485 		    sc->sc_dev.dv_xname, len);
486 		return (1);
487 	}
488 
489 	usbd_setup_xfer(sc->sc_xfer_out, sc->sc_ph_obulk, sc, (void *)buf,
490 	    len, USBD_SYNCHRONOUS, UOW_TIMEOUT, NULL);
491 	error = usbd_transfer(sc->sc_xfer_out);
492 	if (error != 0) {
493 		printf("%s: write failed, len %d: %s\n",
494 		    sc->sc_dev.dv_xname, len, usbd_errstr(error));
495 		uow_reset(sc);
496 		return (1);
497 	}
498 
499 	return (0);
500 }
501 
502 int
503 uow_reset(struct uow_softc *sc)
504 {
505 	return (uow_ctlcmd(sc, DS2490_CTL_RESET_DEVICE, 0));
506 }
507