1 /* $NetBSD: uslsa.c,v 1.29 2020/03/13 18:17:41 christos Exp $ */ 2 3 /* from ugensa.c */ 4 5 /* 6 * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Roland C. Dowdeswell <elric@netbsd.org>. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 2007, 2009 Jonathan A. Kollasch. 36 * All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 57 * 58 */ 59 60 #include <sys/cdefs.h> 61 __KERNEL_RCSID(0, "$NetBSD: uslsa.c,v 1.29 2020/03/13 18:17:41 christos Exp $"); 62 63 #ifdef _KERNEL_OPT 64 #include "opt_usb.h" 65 #endif 66 67 #include <sys/param.h> 68 #include <sys/systm.h> 69 #include <sys/kernel.h> 70 #include <sys/device.h> 71 #include <sys/conf.h> 72 #include <sys/tty.h> 73 74 #include <dev/usb/usb.h> 75 76 #include <dev/usb/usbdi.h> 77 #include <dev/usb/usbdi_util.h> 78 #include <dev/usb/usbdevs.h> 79 80 #include <dev/usb/ucomvar.h> 81 82 #include <dev/usb/uslsareg.h> 83 84 #include <fs/unicode.h> 85 86 #ifdef USLSA_DEBUG 87 #define DPRINTF(x) if (uslsadebug) device_printf x 88 int uslsadebug = 0; 89 #else 90 #define DPRINTF(x) 91 #endif 92 93 struct uslsa_softc { 94 device_t sc_dev; /* base device */ 95 device_t sc_subdev; /* ucom device */ 96 struct usbd_device * sc_udev; /* usb device */ 97 struct usbd_interface * sc_iface; /* interface */ 98 uint8_t sc_ifnum; /* interface number */ 99 bool sc_dying; /* disconnecting */ 100 }; 101 102 static void uslsa_get_status(void *sc, int, u_char *, u_char *); 103 static void uslsa_set(void *, int, int, int); 104 static int uslsa_param(void *, int, struct termios *); 105 static int uslsa_ioctl(void *, int, u_long, void *, int, proc_t *); 106 static int uslsa_open(void *, int); 107 static void uslsa_close(void *, int); 108 109 static int uslsa_usbd_errno(usbd_status); 110 static int uslsa_request_set(struct uslsa_softc *, uint8_t, uint16_t); 111 static int uslsa_set_flow(struct uslsa_softc *, tcflag_t, tcflag_t); 112 113 static const struct ucom_methods uslsa_methods = { 114 .ucom_get_status = uslsa_get_status, 115 .ucom_set = uslsa_set, 116 .ucom_param = uslsa_param, 117 .ucom_ioctl = uslsa_ioctl, 118 .ucom_open = uslsa_open, 119 .ucom_close = uslsa_close, 120 }; 121 122 #define USLSA_CONFIG_INDEX 0 123 #define USLSA_IFACE_INDEX 0 124 #define USLSA_BUFSIZE 256 125 126 static const struct usb_devno uslsa_devs[] = { 127 { USB_VENDOR_BALTECH, USB_PRODUCT_BALTECH_CARDREADER }, 128 { USB_VENDOR_DYNASTREAM, USB_PRODUCT_DYNASTREAM_ANTDEVBOARD }, 129 { USB_VENDOR_JABLOTRON, USB_PRODUCT_JABLOTRON_PC60B }, 130 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_ARGUSISP }, 131 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CRUMB128 }, 132 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DEGREECONT }, 133 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DESKTOPMOBILE }, 134 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_IPLINK1220 }, 135 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_HARP }, 136 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_JTAG }, 137 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_LIN }, 138 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_POLOLU }, 139 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_1 }, 140 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_2 }, 141 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_SUNNTO }, 142 { USB_VENDOR_SILABS2, USB_PRODUCT_SILABS2_DCU11CLONE }, 143 { USB_VENDOR_USI, USB_PRODUCT_USI_MC60 }, 144 { USB_VENDOR_WMR, USB_PRODUCT_WMR_RIGBLASTER }, 145 }; 146 147 static int uslsa_match(device_t, cfdata_t, void *); 148 static void uslsa_attach(device_t, device_t, void *); 149 static void uslsa_childdet(device_t, device_t); 150 static int uslsa_detach(device_t, int); 151 152 CFATTACH_DECL2_NEW(uslsa, sizeof(struct uslsa_softc), uslsa_match, 153 uslsa_attach, uslsa_detach, NULL, NULL, uslsa_childdet); 154 155 static int 156 uslsa_match(device_t parent, cfdata_t match, void *aux) 157 { 158 const struct usbif_attach_arg *uiaa = aux; 159 160 if (usb_lookup(uslsa_devs, uiaa->uiaa_vendor, uiaa->uiaa_product) 161 != NULL) 162 return UMATCH_VENDOR_PRODUCT; 163 else 164 return UMATCH_NONE; 165 } 166 167 static void 168 uslsa_attach(device_t parent, device_t self, void *aux) 169 { 170 struct uslsa_softc *sc; 171 const struct usbif_attach_arg *uiaa = aux; 172 const usb_interface_descriptor_t *id; 173 const usb_endpoint_descriptor_t *ed; 174 char *devinfop; 175 struct ucom_attach_args ucaa; 176 int i; 177 178 sc = device_private(self); 179 180 sc->sc_dev = self; 181 sc->sc_udev = uiaa->uiaa_device; 182 sc->sc_iface = uiaa->uiaa_iface; 183 sc->sc_dying = false; 184 185 aprint_naive("\n"); 186 aprint_normal("\n"); 187 188 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0); 189 aprint_normal_dev(self, "%s\n", devinfop); 190 usbd_devinfo_free(devinfop); 191 192 id = usbd_get_interface_descriptor(sc->sc_iface); 193 194 sc->sc_ifnum = id->bInterfaceNumber; 195 196 ucaa.ucaa_info = "Silicon Labs CP210x"; 197 ucaa.ucaa_portno = UCOM_UNK_PORTNO; 198 ucaa.ucaa_ibufsize = USLSA_BUFSIZE; 199 ucaa.ucaa_obufsize = USLSA_BUFSIZE; 200 ucaa.ucaa_ibufsizepad = USLSA_BUFSIZE; 201 ucaa.ucaa_opkthdrlen = 0; 202 ucaa.ucaa_device = sc->sc_udev; 203 ucaa.ucaa_iface = sc->sc_iface; 204 ucaa.ucaa_methods = &uslsa_methods; 205 ucaa.ucaa_arg = sc; 206 207 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 208 209 ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1; 210 for (i = 0; i < id->bNumEndpoints; i++) { 211 int addr, dir, attr; 212 213 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 214 if (ed == NULL) { 215 aprint_error_dev(self, 216 "could not read endpoint descriptor\n"); 217 sc->sc_dying = true; 218 return; 219 } 220 addr = ed->bEndpointAddress; 221 dir = UE_GET_DIR(ed->bEndpointAddress); 222 attr = ed->bmAttributes & UE_XFERTYPE; 223 if (dir == UE_DIR_IN && attr == UE_BULK) { 224 ucaa.ucaa_bulkin = addr; 225 } else if (dir == UE_DIR_OUT && attr == UE_BULK) { 226 ucaa.ucaa_bulkout = addr; 227 } else { 228 aprint_error_dev(self, "unexpected endpoint\n"); 229 } 230 } 231 aprint_debug_dev(sc->sc_dev, "EPs: in=%#x out=%#x\n", 232 ucaa.ucaa_bulkin, ucaa.ucaa_bulkout); 233 if ((ucaa.ucaa_bulkin == -1) || (ucaa.ucaa_bulkout == -1)) { 234 aprint_error_dev(self, "could not find endpoints\n"); 235 sc->sc_dying = true; 236 return; 237 } 238 239 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa, 240 ucomprint, ucomsubmatch); 241 242 if (!pmf_device_register(self, NULL, NULL)) 243 aprint_error_dev(self, "couldn't establish power handler\n"); 244 245 return; 246 } 247 248 static void 249 uslsa_childdet(device_t self, device_t child) 250 { 251 struct uslsa_softc *sc = device_private(self); 252 253 KASSERT(sc->sc_subdev == child); 254 sc->sc_subdev = NULL; 255 } 256 257 static int 258 uslsa_detach(device_t self, int flags) 259 { 260 struct uslsa_softc *sc = device_private(self); 261 int rv = 0; 262 263 DPRINTF((self, "%s(%p, %#x)\n", __func__, self, flags)); 264 265 sc->sc_dying = true; 266 267 if (sc->sc_subdev != NULL) { 268 rv = config_detach(sc->sc_subdev, flags); 269 sc->sc_subdev = NULL; 270 } 271 272 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 273 274 return rv; 275 } 276 277 static int 278 uslsa_usbd_errno(usbd_status status) 279 { 280 switch (status) { 281 case USBD_NORMAL_COMPLETION: 282 return 0; 283 case USBD_STALLED: 284 return EINVAL; 285 default: 286 return EIO; 287 } 288 } 289 290 static void 291 uslsa_get_status(void *vsc, int portno, u_char *lsr, u_char *msr) 292 { 293 struct uslsa_softc *sc; 294 usb_device_request_t req; 295 usbd_status status; 296 uint8_t mdmsts; 297 298 sc = vsc; 299 300 DPRINTF((sc->sc_dev, "%s(%p, %d, ....)\n", __func__, vsc, portno)); 301 302 if (sc->sc_dying) 303 return; 304 305 req.bmRequestType = UT_READ_VENDOR_INTERFACE; 306 req.bRequest = SLSA_R_GET_MDMSTS; 307 USETW(req.wValue, 0); 308 USETW(req.wIndex, sc->sc_ifnum); 309 USETW(req.wLength, SLSA_RL_GET_MDMSTS); 310 311 status = usbd_do_request(sc->sc_udev, &req, &mdmsts); 312 if (status != USBD_NORMAL_COMPLETION) { 313 device_printf(sc->sc_dev, "%s: GET_MDMSTS %s\n", 314 __func__, usbd_errstr(status)); 315 return; 316 } 317 318 DPRINTF((sc->sc_dev, "%s: GET_MDMSTS %#x\n", __func__, mdmsts)); 319 320 *lsr = 0; 321 322 *msr = ISSET(mdmsts, SLSA_MDMSTS_CTS) ? UMSR_CTS : 0; 323 *msr |= ISSET(mdmsts, SLSA_MDMSTS_DSR) ? UMSR_DSR : 0; 324 *msr |= ISSET(mdmsts, SLSA_MDMSTS_RI) ? UMSR_RI : 0; 325 *msr |= ISSET(mdmsts, SLSA_MDMSTS_DCD) ? UMSR_DCD : 0; 326 } 327 328 static void 329 uslsa_set(void *vsc, int portno, int reg, int onoff) 330 { 331 struct uslsa_softc *sc; 332 333 sc = vsc; 334 335 DPRINTF((sc->sc_dev, "%s(%p, %d, %d, %d)\n", __func__, vsc, portno, 336 reg, onoff)); 337 338 if (sc->sc_dying) 339 return; 340 341 switch (reg) { 342 case UCOM_SET_DTR: 343 if (uslsa_request_set(sc, SLSA_R_SET_MHS, 344 SLSA_RV_SET_MHS_DTR_MASK | 345 (onoff ? SLSA_RV_SET_MHS_DTR : 0))) { 346 device_printf(sc->sc_dev, "SET_MHS/DTR failed\n"); 347 } 348 break; 349 case UCOM_SET_RTS: 350 if (uslsa_request_set(sc, SLSA_R_SET_MHS, 351 SLSA_RV_SET_MHS_RTS_MASK | 352 (onoff ? SLSA_RV_SET_MHS_RTS : 0))) { 353 device_printf(sc->sc_dev, "SET_MHS/RTS failed\n"); 354 } 355 break; 356 case UCOM_SET_BREAK: 357 if (uslsa_request_set(sc, SLSA_R_SET_BREAK, 358 (onoff ? SLSA_RV_SET_BREAK_ENABLE : 359 SLSA_RV_SET_BREAK_DISABLE))) { 360 device_printf(sc->sc_dev, "SET_BREAK failed\n"); 361 } 362 break; 363 default: 364 break; 365 } 366 } 367 368 static int 369 uslsa_param(void *vsc, int portno, struct termios *t) 370 { 371 struct uslsa_softc *sc; 372 usb_device_request_t req; 373 usbd_status status; 374 uint16_t value; 375 uint32_t baud; 376 int ret; 377 378 sc = vsc; 379 380 DPRINTF((sc->sc_dev, "%s(%p, %d, %p)\n", __func__, vsc, portno, t)); 381 382 if (sc->sc_dying) 383 return EIO; 384 385 req.bmRequestType = UT_WRITE_VENDOR_INTERFACE; 386 req.bRequest = SLSA_R_SET_BAUDRATE; 387 USETW(req.wValue, 0); 388 USETW(req.wIndex, sc->sc_ifnum); 389 USETW(req.wLength, 4); 390 391 baud = t->c_ospeed; 392 status = usbd_do_request(sc->sc_udev, &req, &baud); 393 if (status != USBD_NORMAL_COMPLETION) { 394 /* fallback method for devices that don't know SET_BAUDRATE */ 395 /* hope we calculate it right */ 396 device_printf(sc->sc_dev, "%s: set baudrate %d, failed %s," 397 " using set bauddiv\n", 398 __func__, baud, usbd_errstr(status)); 399 400 value = SLSA_RV_BAUDDIV(t->c_ospeed); 401 if ((ret = uslsa_request_set(sc, SLSA_R_SET_BAUDDIV, value)) 402 != 0) { 403 device_printf(sc->sc_dev, "%s: SET_BAUDDIV failed\n", 404 __func__); 405 return ret; 406 } 407 } 408 409 value = 0; 410 411 if (ISSET(t->c_cflag, CSTOPB)) { 412 value |= SLSA_RV_LINE_CTL_STOP_2; 413 } else { 414 value |= SLSA_RV_LINE_CTL_STOP_1; 415 } 416 417 if (ISSET(t->c_cflag, PARENB)) { 418 if (ISSET(t->c_cflag, PARODD)) { 419 value |= SLSA_RV_LINE_CTL_PARITY_ODD; 420 } else { 421 value |= SLSA_RV_LINE_CTL_PARITY_EVEN; 422 } 423 } else { 424 value |= SLSA_RV_LINE_CTL_PARITY_NONE; 425 } 426 427 switch (ISSET(t->c_cflag, CSIZE)) { 428 case CS5: 429 value |= SLSA_RV_LINE_CTL_LEN_5; 430 break; 431 case CS6: 432 value |= SLSA_RV_LINE_CTL_LEN_6; 433 break; 434 case CS7: 435 value |= SLSA_RV_LINE_CTL_LEN_7; 436 break; 437 case CS8: 438 value |= SLSA_RV_LINE_CTL_LEN_8; 439 break; 440 } 441 442 DPRINTF((sc->sc_dev, "%s: setting LINE_CTL to %#x\n", 443 __func__, value)); 444 if ((ret = uslsa_request_set(sc, SLSA_R_SET_LINE_CTL, value)) != 0) { 445 device_printf(sc->sc_dev, "SET_LINE_CTL failed\n"); 446 return ret; 447 } 448 449 if ((ret = uslsa_set_flow(sc, t->c_cflag, t->c_iflag)) != 0) { 450 device_printf(sc->sc_dev, "SET_LINE_CTL failed\n"); 451 } 452 453 return ret; 454 } 455 456 static int 457 uslsa_ioctl(void *vsc, int portno, u_long cmd, void *data, int flag, proc_t *p) 458 { 459 struct uslsa_softc *sc; 460 461 sc = vsc; 462 463 if (sc->sc_dying) 464 return EIO; 465 466 switch (cmd) { 467 case TIOCMGET: 468 ucom_status_change(device_private(sc->sc_subdev)); 469 return EPASSTHROUGH; 470 default: 471 return EPASSTHROUGH; 472 } 473 474 return 0; 475 } 476 477 static int 478 uslsa_open(void *vsc, int portno) 479 { 480 struct uslsa_softc *sc; 481 482 sc = vsc; 483 484 DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno)); 485 486 if (sc->sc_dying) 487 return EIO; 488 489 return uslsa_request_set(sc, SLSA_R_IFC_ENABLE, 490 SLSA_RV_IFC_ENABLE_ENABLE); 491 } 492 493 static void 494 uslsa_close(void *vsc, int portno) 495 { 496 struct uslsa_softc *sc; 497 498 sc = vsc; 499 500 DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno)); 501 502 if (sc->sc_dying) 503 return; 504 505 uslsa_request_set(sc, SLSA_R_IFC_ENABLE, SLSA_RV_IFC_ENABLE_DISABLE); 506 } 507 508 static int 509 uslsa_request_set(struct uslsa_softc * sc, uint8_t request, uint16_t value) 510 { 511 usb_device_request_t req; 512 usbd_status status; 513 514 req.bmRequestType = UT_WRITE_VENDOR_INTERFACE; 515 req.bRequest = request; 516 USETW(req.wValue, value); 517 USETW(req.wIndex, sc->sc_ifnum); 518 USETW(req.wLength, 0); 519 520 status = usbd_do_request(sc->sc_udev, &req, NULL); 521 522 return uslsa_usbd_errno(status); 523 } 524 525 static int 526 uslsa_set_flow(struct uslsa_softc *sc, tcflag_t cflag, tcflag_t iflag) 527 { 528 struct slsa_fcs fcs; 529 usb_device_request_t req; 530 uint32_t ulControlHandshake; 531 uint32_t ulFlowReplace; 532 usbd_status status; 533 534 DPRINTF((sc->sc_dev, "%s(%p, %#x, %#x)\n", __func__, sc, cflag, iflag)); 535 536 req.bmRequestType = UT_READ_VENDOR_INTERFACE; 537 req.bRequest = SLSA_R_GET_FLOW; 538 USETW(req.wValue, 0); 539 USETW(req.wIndex, sc->sc_ifnum); 540 USETW(req.wLength, SLSA_RL_GET_FLOW); 541 542 status = usbd_do_request(sc->sc_udev, &req, &fcs); 543 if (status != USBD_NORMAL_COMPLETION) { 544 device_printf(sc->sc_dev, "%s: GET_FLOW %s\n", 545 __func__, usbd_errstr(status)); 546 return uslsa_usbd_errno(status); 547 } 548 549 ulControlHandshake = le32toh(fcs.ulControlHandshake); 550 ulFlowReplace = le32toh(fcs.ulFlowReplace); 551 552 if (ISSET(cflag, CRTSCTS)) { 553 ulControlHandshake = 554 SERIAL_CTS_HANDSHAKE | __SHIFTIN(1, SERIAL_DTR_MASK); 555 ulFlowReplace = __SHIFTIN(2, SERIAL_RTS_MASK); 556 } else { 557 ulControlHandshake = __SHIFTIN(1, SERIAL_DTR_MASK); 558 ulFlowReplace = __SHIFTIN(1, SERIAL_RTS_MASK); 559 } 560 561 fcs.ulControlHandshake = htole32(ulControlHandshake); 562 fcs.ulFlowReplace = htole32(ulFlowReplace); 563 564 req.bmRequestType = UT_WRITE_VENDOR_INTERFACE; 565 req.bRequest = SLSA_R_SET_FLOW; 566 USETW(req.wValue, 0); 567 USETW(req.wIndex, sc->sc_ifnum); 568 USETW(req.wLength, SLSA_RL_SET_FLOW); 569 570 status = usbd_do_request(sc->sc_udev, &req, &fcs); 571 572 return uslsa_usbd_errno(status); 573 } 574