1 /* $OpenBSD: uow.c,v 1.34 2015/08/31 07:32:15 mpi 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) 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 /* Set USB configuration */ 125 if ((error = usbd_set_config_no(sc->sc_udev, 126 DS2490_USB_CONFIG, 0)) != 0) { 127 printf("%s: failed to set config %d: %s\n", 128 sc->sc_dev.dv_xname, DS2490_USB_CONFIG, 129 usbd_errstr(error)); 130 return; 131 } 132 133 /* Get interface handle */ 134 if ((error = usbd_device2interface_handle(sc->sc_udev, 135 DS2490_USB_IFACE, &sc->sc_iface)) != 0) { 136 printf("%s: failed to get iface %d: %s\n", 137 sc->sc_dev.dv_xname, DS2490_USB_IFACE, 138 usbd_errstr(error)); 139 return; 140 } 141 142 /* Find endpoints */ 143 id = usbd_get_interface_descriptor(sc->sc_iface); 144 for (i = 0; i < id->bNumEndpoints; i++) { 145 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 146 if (ed == NULL) { 147 printf("%s: failed to get endpoint %d descriptor\n", 148 sc->sc_dev.dv_xname, i); 149 return; 150 } 151 152 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 153 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 154 ep_ibulk = ed->bEndpointAddress; 155 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 156 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) 157 ep_obulk = ed->bEndpointAddress; 158 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 159 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) 160 ep_intr = ed->bEndpointAddress; 161 } 162 if (ep_ibulk == -1 || ep_obulk == -1 || ep_intr == -1) { 163 printf("%s: missing endpoint: ibulk %d, obulk %d, intr %d\n", 164 sc->sc_dev.dv_xname, ep_ibulk, ep_obulk, ep_intr); 165 return; 166 } 167 168 /* Open pipes */ 169 if ((error = usbd_open_pipe(sc->sc_iface, ep_ibulk, USBD_EXCLUSIVE_USE, 170 &sc->sc_ph_ibulk)) != 0) { 171 printf("%s: failed to open bulk-in pipe: %s\n", 172 sc->sc_dev.dv_xname, usbd_errstr(error)); 173 return; 174 } 175 if ((error = usbd_open_pipe(sc->sc_iface, ep_obulk, USBD_EXCLUSIVE_USE, 176 &sc->sc_ph_obulk)) != 0) { 177 printf("%s: failed to open bulk-out pipe: %s\n", 178 sc->sc_dev.dv_xname, usbd_errstr(error)); 179 goto fail; 180 } 181 if ((error = usbd_open_pipe_intr(sc->sc_iface, ep_intr, 182 USBD_SHORT_XFER_OK, &sc->sc_ph_intr, sc, 183 sc->sc_regs, sizeof(sc->sc_regs), uow_intr, 184 USBD_DEFAULT_INTERVAL)) != 0) { 185 printf("%s: failed to open intr pipe: %s\n", 186 sc->sc_dev.dv_xname, usbd_errstr(error)); 187 goto fail; 188 } 189 190 /* Allocate xfers for bulk transfers */ 191 if ((sc->sc_xfer_in = usbd_alloc_xfer(sc->sc_udev)) == NULL) { 192 printf("%s: failed to alloc bulk-in xfer\n", 193 sc->sc_dev.dv_xname); 194 goto fail; 195 } 196 if ((sc->sc_xfer_out = usbd_alloc_xfer(sc->sc_udev)) == NULL) { 197 printf("%s: failed to alloc bulk-out xfer\n", 198 sc->sc_dev.dv_xname); 199 goto fail; 200 } 201 202 memset(sc->sc_fifo, 0xff, sizeof(sc->sc_fifo)); 203 204 /* Reset device */ 205 uow_reset(sc); 206 207 /* Attach 1-Wire bus */ 208 sc->sc_ow_bus.bus_cookie = sc; 209 sc->sc_ow_bus.bus_reset = uow_ow_reset; 210 sc->sc_ow_bus.bus_bit = uow_ow_bit; 211 sc->sc_ow_bus.bus_read_byte = uow_ow_read_byte; 212 sc->sc_ow_bus.bus_write_byte = uow_ow_write_byte; 213 sc->sc_ow_bus.bus_read_block = uow_ow_read_block; 214 sc->sc_ow_bus.bus_write_block = uow_ow_write_block; 215 sc->sc_ow_bus.bus_matchrom = uow_ow_matchrom; 216 #if 0 217 sc->sc_ow_bus.bus_search = uow_ow_search; 218 #endif 219 220 bzero(&oba, sizeof(oba)); 221 oba.oba_bus = &sc->sc_ow_bus; 222 sc->sc_ow_dev = config_found(self, &oba, onewirebus_print); 223 224 return; 225 226 fail: 227 if (sc->sc_ph_ibulk != NULL) { 228 usbd_close_pipe(sc->sc_ph_ibulk); 229 sc->sc_ph_ibulk = NULL; 230 } 231 if (sc->sc_ph_obulk != NULL) { 232 usbd_close_pipe(sc->sc_ph_obulk); 233 sc->sc_ph_obulk = NULL; 234 } 235 if (sc->sc_ph_intr != NULL) { 236 usbd_close_pipe(sc->sc_ph_intr); 237 sc->sc_ph_intr = NULL; 238 } 239 if (sc->sc_xfer_in != NULL) { 240 usbd_free_xfer(sc->sc_xfer_in); 241 sc->sc_xfer_in = NULL; 242 } 243 if (sc->sc_xfer_out != NULL) { 244 usbd_free_xfer(sc->sc_xfer_out); 245 sc->sc_xfer_out = NULL; 246 } 247 } 248 249 int 250 uow_detach(struct device *self, int flags) 251 { 252 struct uow_softc *sc = (struct uow_softc *)self; 253 int rv = 0, s; 254 255 s = splusb(); 256 257 if (sc->sc_ph_ibulk != NULL) { 258 usbd_abort_pipe(sc->sc_ph_ibulk); 259 usbd_close_pipe(sc->sc_ph_ibulk); 260 } 261 if (sc->sc_ph_obulk != NULL) { 262 usbd_abort_pipe(sc->sc_ph_obulk); 263 usbd_close_pipe(sc->sc_ph_obulk); 264 } 265 if (sc->sc_ph_intr != NULL) { 266 usbd_abort_pipe(sc->sc_ph_intr); 267 usbd_close_pipe(sc->sc_ph_intr); 268 } 269 270 if (sc->sc_xfer_in != NULL) 271 usbd_free_xfer(sc->sc_xfer_in); 272 if (sc->sc_xfer_out != NULL) 273 usbd_free_xfer(sc->sc_xfer_out); 274 275 if (sc->sc_ow_dev != NULL) 276 rv = config_detach(sc->sc_ow_dev, flags); 277 278 splx(s); 279 280 return (rv); 281 } 282 283 int 284 uow_activate(struct device *self, int act) 285 { 286 struct uow_softc *sc = (struct uow_softc *)self; 287 int rv = 0; 288 289 switch (act) { 290 case DVACT_DEACTIVATE: 291 if (sc->sc_ow_dev != NULL) 292 rv = config_deactivate(sc->sc_ow_dev); 293 usbd_deactivate(sc->sc_udev); 294 break; 295 } 296 297 return (rv); 298 } 299 300 int 301 uow_ow_reset(void *arg) 302 { 303 struct uow_softc *sc = arg; 304 305 if (uow_commcmd(sc, DS2490_COMM_1WIRE_RESET | DS2490_BIT_IM, 0) != 0) 306 return (1); 307 308 /* XXX: check presence pulse */ 309 return (0); 310 } 311 312 int 313 uow_ow_bit(void *arg, int value) 314 { 315 struct uow_softc *sc = arg; 316 u_int8_t data; 317 318 if (uow_commcmd(sc, DS2490_COMM_BIT_IO | DS2490_BIT_IM | 319 (value ? DS2490_BIT_D : 0), 0) != 0) 320 return (1); 321 if (uow_read(sc, &data, 1) != 1) 322 return (1); 323 324 return (data); 325 } 326 327 int 328 uow_ow_read_byte(void *arg) 329 { 330 struct uow_softc *sc = arg; 331 u_int8_t data; 332 333 if (uow_commcmd(sc, DS2490_COMM_BYTE_IO | DS2490_BIT_IM, 0xff) != 0) 334 return (-1); 335 if (uow_read(sc, &data, 1) != 1) 336 return (-1); 337 338 return (data); 339 } 340 341 void 342 uow_ow_write_byte(void *arg, int value) 343 { 344 struct uow_softc *sc = arg; 345 u_int8_t data; 346 347 if (uow_commcmd(sc, DS2490_COMM_BYTE_IO | DS2490_BIT_IM, value) != 0) 348 return; 349 uow_read(sc, &data, sizeof(data)); 350 } 351 352 void 353 uow_ow_read_block(void *arg, void *buf, int len) 354 { 355 struct uow_softc *sc = arg; 356 357 if (uow_write(sc, sc->sc_fifo, len) != 0) 358 return; 359 if (uow_commcmd(sc, DS2490_COMM_BLOCK_IO | DS2490_BIT_IM, len) != 0) 360 return; 361 uow_read(sc, buf, len); 362 } 363 364 void 365 uow_ow_write_block(void *arg, const void *buf, int len) 366 { 367 struct uow_softc *sc = arg; 368 369 if (uow_write(sc, buf, len) != 0) 370 return; 371 if (uow_commcmd(sc, DS2490_COMM_BLOCK_IO | DS2490_BIT_IM, len) != 0) 372 return; 373 } 374 375 void 376 uow_ow_matchrom(void *arg, u_int64_t rom) 377 { 378 struct uow_softc *sc = arg; 379 u_int8_t data[8]; 380 int i; 381 382 for (i = 0; i < 8; i++) 383 data[i] = (rom >> (i * 8)) & 0xff; 384 385 if (uow_write(sc, data, 8) != 0) 386 return; 387 if (uow_commcmd(sc, DS2490_COMM_MATCH_ACCESS | DS2490_BIT_IM, 388 ONEWIRE_CMD_MATCH_ROM) != 0) 389 return; 390 } 391 392 int 393 uow_ow_search(void *arg, u_int64_t *buf, int size, u_int64_t startrom) 394 { 395 struct uow_softc *sc = arg; 396 u_int8_t data[8]; 397 int i, rv; 398 399 for (i = 0; i < 8; i++) 400 data[i] = (startrom >> (i * 8)) & 0xff; 401 402 if (uow_write(sc, data, 8) != 0) 403 return (-1); 404 if (uow_commcmd(sc, DS2490_COMM_SEARCH_ACCESS | DS2490_BIT_IM | 405 DS2490_BIT_SM | DS2490_BIT_RST | DS2490_BIT_F, size << 8 | 406 ONEWIRE_CMD_SEARCH_ROM) != 0) 407 return (-1); 408 409 if ((rv = uow_read(sc, buf, size * 8)) == -1) 410 return (-1); 411 412 return (rv / 8); 413 } 414 415 int 416 uow_cmd(struct uow_softc *sc, int type, int cmd, int param) 417 { 418 usb_device_request_t req; 419 usbd_status error; 420 421 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 422 req.bRequest = type; 423 USETW(req.wValue, cmd); 424 USETW(req.wIndex, param); 425 USETW(req.wLength, 0); 426 if ((error = usbd_do_request(sc->sc_udev, &req, NULL)) != 0) { 427 printf("%s: cmd failed, type 0x%02x, cmd 0x%04x, " 428 "param 0x%04x: %s\n", sc->sc_dev.dv_xname, type, cmd, 429 param, usbd_errstr(error)); 430 if (cmd != DS2490_CTL_RESET_DEVICE) 431 uow_reset(sc); 432 return (1); 433 } 434 435 again: 436 if (tsleep(sc->sc_regs, PRIBIO, "uowcmd", 437 (UOW_TIMEOUT * hz) / 1000) != 0) { 438 printf("%s: cmd timeout, type 0x%02x, cmd 0x%04x, " 439 "param 0x%04x\n", sc->sc_dev.dv_xname, type, cmd, 440 param); 441 return (1); 442 } 443 if ((sc->sc_regs[DS2490_ST_STFL] & DS2490_ST_STFL_IDLE) == 0) 444 goto again; 445 446 return (0); 447 } 448 449 void 450 uow_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) 451 { 452 struct uow_softc *sc = priv; 453 454 if (status != USBD_NORMAL_COMPLETION) { 455 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 456 return; 457 if (status == USBD_STALLED) 458 usbd_clear_endpoint_stall_async(sc->sc_ph_intr); 459 return; 460 } 461 462 wakeup(sc->sc_regs); 463 } 464 465 int 466 uow_read(struct uow_softc *sc, void *buf, int len) 467 { 468 usbd_status error; 469 int count; 470 471 /* XXX: implement FIFO status monitoring */ 472 if (len > DS2490_DATAFIFOSIZE) { 473 printf("%s: read %d bytes, xfer too big\n", 474 sc->sc_dev.dv_xname, len); 475 return (-1); 476 } 477 478 usbd_setup_xfer(sc->sc_xfer_in, sc->sc_ph_ibulk, sc, buf, len, 479 USBD_SHORT_XFER_OK | USBD_SYNCHRONOUS, UOW_TIMEOUT, NULL); 480 error = usbd_transfer(sc->sc_xfer_in); 481 if (error != 0) { 482 printf("%s: read failed, len %d: %s\n", 483 sc->sc_dev.dv_xname, len, usbd_errstr(error)); 484 uow_reset(sc); 485 return (-1); 486 } 487 488 usbd_get_xfer_status(sc->sc_xfer_in, NULL, NULL, &count, &error); 489 return (count); 490 } 491 492 int 493 uow_write(struct uow_softc *sc, const void *buf, int len) 494 { 495 usbd_status error; 496 497 /* XXX: implement FIFO status monitoring */ 498 if (len > DS2490_DATAFIFOSIZE) { 499 printf("%s: write %d bytes, xfer too big\n", 500 sc->sc_dev.dv_xname, len); 501 return (1); 502 } 503 504 usbd_setup_xfer(sc->sc_xfer_out, sc->sc_ph_obulk, sc, (void *)buf, 505 len, USBD_SYNCHRONOUS, UOW_TIMEOUT, NULL); 506 error = usbd_transfer(sc->sc_xfer_out); 507 if (error != 0) { 508 printf("%s: write failed, len %d: %s\n", 509 sc->sc_dev.dv_xname, len, usbd_errstr(error)); 510 uow_reset(sc); 511 return (1); 512 } 513 514 return (0); 515 } 516 517 int 518 uow_reset(struct uow_softc *sc) 519 { 520 return (uow_ctlcmd(sc, DS2490_CTL_RESET_DEVICE, 0)); 521 } 522