1 /* $NetBSD: uslsa.c,v 1.19 2014/03/10 19:55:18 reinoud 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.19 2014/03/10 19:55:18 reinoud Exp $"); 62 63 #include <sys/param.h> 64 #include <sys/systm.h> 65 #include <sys/kernel.h> 66 #include <sys/device.h> 67 #include <sys/conf.h> 68 #include <sys/tty.h> 69 70 #include <dev/usb/usb.h> 71 72 #include <dev/usb/usbdi.h> 73 #include <dev/usb/usbdi_util.h> 74 #include <dev/usb/usbdevs.h> 75 76 #include <dev/usb/ucomvar.h> 77 78 #include <dev/usb/uslsareg.h> 79 80 #include <fs/unicode.h> 81 82 #ifdef USLSA_DEBUG 83 #define DPRINTF(x) if (uslsadebug) device_printf x 84 int uslsadebug = 0; 85 #else 86 #define DPRINTF(x) 87 #endif 88 89 struct uslsa_softc { 90 device_t sc_dev; /* base device */ 91 device_t sc_subdev; /* ucom device */ 92 usbd_device_handle sc_udev; /* usb device */ 93 usbd_interface_handle sc_iface; /* interface */ 94 uint8_t sc_ifnum; /* interface number */ 95 bool sc_dying; /* disconnecting */ 96 }; 97 98 static void uslsa_get_status(void *sc, int, u_char *, u_char *); 99 static void uslsa_set(void *, int, int, int); 100 static int uslsa_param(void *, int, struct termios *); 101 static int uslsa_ioctl(void *, int, u_long, void *, int, proc_t *); 102 103 static int uslsa_open(void *, int); 104 static void uslsa_close(void *, int); 105 106 static int uslsa_usbd_errno(usbd_status); 107 static int uslsa_request_set(struct uslsa_softc *, uint8_t, uint16_t); 108 static int uslsa_set_flow(struct uslsa_softc *, tcflag_t, tcflag_t); 109 110 static const struct ucom_methods uslsa_methods = { 111 uslsa_get_status, 112 uslsa_set, 113 uslsa_param, 114 uslsa_ioctl, 115 uslsa_open, 116 uslsa_close, 117 NULL, 118 NULL, 119 }; 120 121 #define USLSA_CONFIG_INDEX 0 122 #define USLSA_IFACE_INDEX 0 123 #define USLSA_BUFSIZE 256 124 125 static const struct usb_devno uslsa_devs[] = { 126 { USB_VENDOR_BALTECH, USB_PRODUCT_BALTECH_CARDREADER }, 127 { USB_VENDOR_DYNASTREAM, USB_PRODUCT_DYNASTREAM_ANTDEVBOARD }, 128 { USB_VENDOR_JABLOTRON, USB_PRODUCT_JABLOTRON_PC60B }, 129 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_ARGUSISP }, 130 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CRUMB128 }, 131 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DEGREECONT }, 132 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DESKTOPMOBILE }, 133 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_IPLINK1220 }, 134 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_HARP }, 135 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_JTAG }, 136 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_LIN }, 137 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_POLOLU }, 138 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_1 }, 139 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_2 }, 140 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_SUNNTO }, 141 { USB_VENDOR_SILABS2, USB_PRODUCT_SILABS2_DCU11CLONE }, 142 { USB_VENDOR_USI, USB_PRODUCT_USI_MC60 } 143 }; 144 145 static int uslsa_match(device_t, cfdata_t, void *); 146 static void uslsa_attach(device_t, device_t, void *); 147 static void uslsa_childdet(device_t, device_t); 148 static int uslsa_detach(device_t, int); 149 static int uslsa_activate(device_t, enum devact); 150 151 CFATTACH_DECL2_NEW(uslsa, sizeof(struct uslsa_softc), uslsa_match, 152 uslsa_attach, uslsa_detach, uslsa_activate, NULL, uslsa_childdet); 153 154 static int 155 uslsa_match(device_t parent, cfdata_t match, void *aux) 156 { 157 const struct usbif_attach_arg *uaa; 158 159 uaa = aux; 160 161 if (usb_lookup(uslsa_devs, uaa->vendor, uaa->product) != NULL) { 162 return UMATCH_VENDOR_PRODUCT; 163 } else { 164 return UMATCH_NONE; 165 } 166 } 167 168 static void 169 uslsa_attach(device_t parent, device_t self, void *aux) 170 { 171 struct uslsa_softc *sc; 172 const struct usbif_attach_arg *uaa; 173 const usb_interface_descriptor_t *id; 174 const usb_endpoint_descriptor_t *ed; 175 char *devinfop; 176 struct ucom_attach_args uca; 177 int i; 178 179 sc = device_private(self); 180 uaa = aux; 181 182 sc->sc_dev = self; 183 sc->sc_udev = uaa->device; 184 sc->sc_iface = uaa->iface; 185 186 aprint_naive("\n"); 187 aprint_normal("\n"); 188 189 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0); 190 aprint_normal_dev(self, "%s\n", devinfop); 191 usbd_devinfo_free(devinfop); 192 193 id = usbd_get_interface_descriptor(sc->sc_iface); 194 195 sc->sc_ifnum = id->bInterfaceNumber; 196 197 uca.info = "Silicon Labs CP210x"; 198 uca.portno = UCOM_UNK_PORTNO; 199 uca.ibufsize = USLSA_BUFSIZE; 200 uca.obufsize = USLSA_BUFSIZE; 201 uca.ibufsizepad = USLSA_BUFSIZE; 202 uca.opkthdrlen = 0; 203 uca.device = sc->sc_udev; 204 uca.iface = sc->sc_iface; 205 uca.methods = &uslsa_methods; 206 uca.arg = sc; 207 208 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 209 sc->sc_dev); 210 211 uca.bulkin = uca.bulkout = -1; 212 for (i = 0; i < id->bNumEndpoints; i++) { 213 int addr, dir, attr; 214 215 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 216 if (ed == NULL) { 217 aprint_error_dev(self, 218 "could not read endpoint descriptor\n"); 219 sc->sc_dying = true; 220 return; 221 } 222 addr = ed->bEndpointAddress; 223 dir = UE_GET_DIR(ed->bEndpointAddress); 224 attr = ed->bmAttributes & UE_XFERTYPE; 225 if (dir == UE_DIR_IN && attr == UE_BULK) { 226 uca.bulkin = addr; 227 } else if (dir == UE_DIR_OUT && attr == UE_BULK) { 228 uca.bulkout = addr; 229 } else { 230 aprint_error_dev(self, "unexpected endpoint\n"); 231 } 232 } 233 aprint_debug_dev(sc->sc_dev, "EPs: in=%#x out=%#x\n", 234 uca.bulkin, uca.bulkout); 235 if ((uca.bulkin == -1) || (uca.bulkout == -1)) { 236 aprint_error_dev(self, "could not find endpoints\n"); 237 sc->sc_dying = true; 238 return; 239 } 240 241 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca, 242 ucomprint, ucomsubmatch); 243 244 return; 245 } 246 247 static int 248 uslsa_activate(device_t self, enum devact act) 249 { 250 struct uslsa_softc *sc = device_private(self); 251 252 switch (act) { 253 case DVACT_DEACTIVATE: 254 sc->sc_dying = true; 255 return 0; 256 default: 257 return EOPNOTSUPP; 258 } 259 } 260 261 static void 262 uslsa_childdet(device_t self, device_t child) 263 { 264 struct uslsa_softc *sc = device_private(self); 265 266 KASSERT(sc->sc_subdev == child); 267 sc->sc_subdev = NULL; 268 } 269 270 static int 271 uslsa_detach(device_t self, int flags) 272 { 273 struct uslsa_softc *sc = device_private(self); 274 int rv = 0; 275 276 DPRINTF((self, "%s(%p, %#x)\n", __func__, self, flags)); 277 278 sc->sc_dying = true; 279 280 if (sc->sc_subdev != NULL) { 281 rv = config_detach(sc->sc_subdev, flags); 282 } 283 284 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 285 sc->sc_dev); 286 287 return (rv); 288 } 289 290 static int 291 uslsa_usbd_errno(usbd_status status) 292 { 293 switch (status) { 294 case USBD_NORMAL_COMPLETION: 295 return 0; 296 case USBD_STALLED: 297 return EINVAL; 298 default: 299 return EIO; 300 } 301 } 302 303 static void 304 uslsa_get_status(void *vsc, int portno, u_char *lsr, u_char *msr) 305 { 306 struct uslsa_softc *sc; 307 usb_device_request_t req; 308 usbd_status status; 309 uint8_t mdmsts; 310 311 sc = vsc; 312 313 DPRINTF((sc->sc_dev, "%s(%p, %d, ....)\n", __func__, vsc, portno)); 314 315 if (sc->sc_dying) { 316 return; 317 } 318 319 req.bmRequestType = UT_READ_VENDOR_INTERFACE; 320 req.bRequest = SLSA_R_GET_MDMSTS; 321 USETW(req.wValue, 0); 322 USETW(req.wIndex, sc->sc_ifnum); 323 USETW(req.wLength, SLSA_RL_GET_MDMSTS); 324 325 status = usbd_do_request(sc->sc_udev, &req, &mdmsts); 326 if (status != USBD_NORMAL_COMPLETION) { 327 device_printf(sc->sc_dev, "%s: GET_MDMSTS %s\n", 328 __func__, usbd_errstr(status)); 329 return; 330 } 331 332 DPRINTF((sc->sc_dev, "%s: GET_MDMSTS %#x\n", __func__, mdmsts)); 333 334 if (lsr != NULL) { 335 *lsr = 0; 336 } 337 338 if (msr != NULL) { 339 *msr = 0; 340 *msr |= ISSET(mdmsts, SLSA_MDMSTS_CTS) ? UMSR_CTS : 0; 341 *msr |= ISSET(mdmsts, SLSA_MDMSTS_DSR) ? UMSR_DSR : 0; 342 *msr |= ISSET(mdmsts, SLSA_MDMSTS_RI) ? UMSR_RI : 0; 343 *msr |= ISSET(mdmsts, SLSA_MDMSTS_DCD) ? UMSR_DCD : 0; 344 } 345 } 346 347 static void 348 uslsa_set(void *vsc, int portno, int reg, int onoff) 349 { 350 struct uslsa_softc *sc; 351 352 sc = vsc; 353 354 DPRINTF((sc->sc_dev, "%s(%p, %d, %d, %d)\n", __func__, vsc, portno, reg, onoff)); 355 356 if (sc->sc_dying) { 357 return; 358 } 359 360 switch (reg) { 361 case UCOM_SET_DTR: 362 if (uslsa_request_set(sc, SLSA_R_SET_MHS, 363 SLSA_RV_SET_MHS_DTR_MASK | 364 (onoff ? SLSA_RV_SET_MHS_DTR : 0))) { 365 device_printf(sc->sc_dev, "SET_MHS/DTR failed\n"); 366 } 367 break; 368 case UCOM_SET_RTS: 369 if (uslsa_request_set(sc, SLSA_R_SET_MHS, 370 SLSA_RV_SET_MHS_RTS_MASK | 371 (onoff ? SLSA_RV_SET_MHS_RTS : 0))) { 372 device_printf(sc->sc_dev, "SET_MHS/RTS failed\n"); 373 } 374 break; 375 case UCOM_SET_BREAK: 376 if (uslsa_request_set(sc, SLSA_R_SET_BREAK, 377 (onoff ? SLSA_RV_SET_BREAK_ENABLE : 378 SLSA_RV_SET_BREAK_DISABLE))) { 379 device_printf(sc->sc_dev, "SET_BREAK failed\n"); 380 } 381 break; 382 default: 383 break; 384 } 385 } 386 387 static int 388 uslsa_param(void *vsc, int portno, struct termios *t) 389 { 390 struct uslsa_softc *sc; 391 usb_device_request_t req; 392 usbd_status status; 393 uint16_t value; 394 uint32_t baud; 395 int ret; 396 397 sc = vsc; 398 399 DPRINTF((sc->sc_dev, "%s(%p, %d, %p)\n", __func__, vsc, portno, t)); 400 401 if (sc->sc_dying) { 402 return EIO; 403 } 404 405 req.bmRequestType = UT_WRITE_VENDOR_INTERFACE; 406 req.bRequest = SLSA_R_SET_BAUDRATE; 407 USETW(req.wValue, 0); 408 USETW(req.wIndex, sc->sc_ifnum); 409 USETW(req.wLength, 4); 410 411 baud = t->c_ospeed; 412 status = usbd_do_request(sc->sc_udev, &req, &baud); 413 if (status != USBD_NORMAL_COMPLETION) { 414 /* fallback method for devices that don't know SET_BAUDRATE */ 415 /* hope we calculate it right */ 416 device_printf(sc->sc_dev, "%s: set baudrate %d, failed %s," 417 " using set bauddiv\n", 418 __func__, baud, usbd_errstr(status)); 419 420 value = SLSA_RV_BAUDDIV(t->c_ospeed); 421 if ((ret = uslsa_request_set(sc, SLSA_R_SET_BAUDDIV, value)) != 0) { 422 device_printf(sc->sc_dev, "%s: SET_BAUDDIV failed\n", 423 __func__); 424 return ret; 425 } 426 } 427 428 value = 0; 429 430 if (ISSET(t->c_cflag, CSTOPB)) { 431 value |= SLSA_RV_LINE_CTL_STOP_2; 432 } else { 433 value |= SLSA_RV_LINE_CTL_STOP_1; 434 } 435 436 if (ISSET(t->c_cflag, PARENB)) { 437 if (ISSET(t->c_cflag, PARODD)) { 438 value |= SLSA_RV_LINE_CTL_PARITY_ODD; 439 } else { 440 value |= SLSA_RV_LINE_CTL_PARITY_EVEN; 441 } 442 } else { 443 value |= SLSA_RV_LINE_CTL_PARITY_NONE; 444 } 445 446 switch (ISSET(t->c_cflag, CSIZE)) { 447 case CS5: 448 value |= SLSA_RV_LINE_CTL_LEN_5; 449 break; 450 case CS6: 451 value |= SLSA_RV_LINE_CTL_LEN_6; 452 break; 453 case CS7: 454 value |= SLSA_RV_LINE_CTL_LEN_7; 455 break; 456 case CS8: 457 value |= SLSA_RV_LINE_CTL_LEN_8; 458 break; 459 } 460 461 DPRINTF((sc->sc_dev, "%s: setting LINE_CTL to 0x%x\n", 462 __func__, value)); 463 if ((ret = uslsa_request_set(sc, SLSA_R_SET_LINE_CTL, value)) != 0) { 464 device_printf(sc->sc_dev, "SET_LINE_CTL failed\n"); 465 return ret; 466 } 467 468 if ((ret = uslsa_set_flow(sc, t->c_cflag, t->c_iflag)) != 0) { 469 device_printf(sc->sc_dev, "SET_LINE_CTL failed\n"); 470 } 471 472 return ret; 473 } 474 475 static int 476 uslsa_ioctl(void *vsc, int portno, u_long cmd, void *data, int flag, proc_t *p) 477 { 478 struct uslsa_softc *sc; 479 480 sc = vsc; 481 482 switch (cmd) { 483 case TIOCMGET: 484 ucom_status_change(device_private(sc->sc_subdev)); 485 return EPASSTHROUGH; 486 default: 487 return EPASSTHROUGH; 488 } 489 490 return 0; 491 } 492 493 static int 494 uslsa_open(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 EIO; 504 } 505 506 return uslsa_request_set(sc, SLSA_R_IFC_ENABLE, 507 SLSA_RV_IFC_ENABLE_ENABLE); 508 } 509 510 static void 511 uslsa_close(void *vsc, int portno) 512 { 513 struct uslsa_softc *sc; 514 515 sc = vsc; 516 517 DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno)); 518 519 if (sc->sc_dying) { 520 return; 521 } 522 523 (void)uslsa_request_set(sc, SLSA_R_IFC_ENABLE, 524 SLSA_RV_IFC_ENABLE_DISABLE); 525 } 526 527 static int 528 uslsa_request_set(struct uslsa_softc * sc, uint8_t request, uint16_t value) 529 { 530 usb_device_request_t req; 531 usbd_status status; 532 533 req.bmRequestType = UT_WRITE_VENDOR_INTERFACE; 534 req.bRequest = request; 535 USETW(req.wValue, value); 536 USETW(req.wIndex, sc->sc_ifnum); 537 USETW(req.wLength, 0); 538 539 status = usbd_do_request(sc->sc_udev, &req, NULL); 540 541 return uslsa_usbd_errno(status); 542 } 543 544 static int 545 uslsa_set_flow(struct uslsa_softc *sc, tcflag_t cflag, tcflag_t iflag) 546 { 547 struct slsa_fcs fcs; 548 usb_device_request_t req; 549 uint32_t ulControlHandshake; 550 uint32_t ulFlowReplace; 551 usbd_status status; 552 553 DPRINTF((sc->sc_dev, "%s(%p, %#x, %#x)\n", __func__, sc, cflag, iflag)); 554 555 req.bmRequestType = UT_READ_VENDOR_INTERFACE; 556 req.bRequest = SLSA_R_GET_FLOW; 557 USETW(req.wValue, 0); 558 USETW(req.wIndex, sc->sc_ifnum); 559 USETW(req.wLength, SLSA_RL_GET_FLOW); 560 561 status = usbd_do_request(sc->sc_udev, &req, &fcs); 562 if (status != USBD_NORMAL_COMPLETION) { 563 device_printf(sc->sc_dev, "%s: GET_FLOW %s\n", 564 __func__, usbd_errstr(status)); 565 return uslsa_usbd_errno(status); 566 } 567 568 ulControlHandshake = le32toh(fcs.ulControlHandshake); 569 ulFlowReplace = le32toh(fcs.ulFlowReplace); 570 571 if (ISSET(cflag, CRTSCTS)) { 572 ulControlHandshake = 573 SERIAL_CTS_HANDSHAKE | __SHIFTIN(1, SERIAL_DTR_MASK); 574 ulFlowReplace = __SHIFTIN(2, SERIAL_RTS_MASK); 575 } else { 576 ulControlHandshake = __SHIFTIN(1, SERIAL_DTR_MASK); 577 ulFlowReplace = __SHIFTIN(1, SERIAL_RTS_MASK); 578 } 579 580 fcs.ulControlHandshake = htole32(ulControlHandshake); 581 fcs.ulFlowReplace = htole32(ulFlowReplace); 582 583 req.bmRequestType = UT_WRITE_VENDOR_INTERFACE; 584 req.bRequest = SLSA_R_SET_FLOW; 585 USETW(req.wValue, 0); 586 USETW(req.wIndex, sc->sc_ifnum); 587 USETW(req.wLength, SLSA_RL_SET_FLOW); 588 589 status = usbd_do_request(sc->sc_udev, &req, &fcs); 590 591 return uslsa_usbd_errno(status); 592 } 593