1 /* $NetBSD: uftdi.c,v 1.77 2024/03/26 03:38:02 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: uftdi.c,v 1.77 2024/03/26 03:38:02 thorpej Exp $"); 34 35 #ifdef _KERNEL_OPT 36 #include "opt_usb.h" 37 #endif 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/device.h> 43 #include <sys/conf.h> 44 #include <sys/tty.h> 45 46 #include <dev/usb/usb.h> 47 48 #include <dev/usb/usbdi.h> 49 #include <dev/usb/usbdi_util.h> 50 #include <dev/usb/usbdivar.h> 51 #include <dev/usb/usbdevs.h> 52 53 #include <dev/usb/ucomvar.h> 54 55 #include <dev/usb/uftdireg.h> 56 57 #ifdef UFTDI_DEBUG 58 #define DPRINTF(x) if (uftdidebug) printf x 59 #define DPRINTFN(n,x) if (uftdidebug>(n)) printf x 60 int uftdidebug = 0; 61 #else 62 #define DPRINTF(x) 63 #define DPRINTFN(n,x) 64 #endif 65 66 #define UFTDI_CONFIG_NO 1 67 68 /* 69 * These are the default number of bytes transferred per frame if the 70 * endpoint doesn't tell us. The output buffer size is a hard limit 71 * for devices that use a 6-bit size encoding. 72 */ 73 #define UFTDIIBUFSIZE 64 74 #define UFTDIOBUFSIZE 64 75 76 /* 77 * Magic constants! Where do these come from? They're what Linux uses... 78 */ 79 #define UFTDI_MAX_IBUFSIZE 512 80 #define UFTDI_MAX_OBUFSIZE 256 81 82 struct uftdi_softc { 83 device_t sc_dev; /* base device */ 84 struct usbd_device * sc_udev; /* device */ 85 struct usbd_interface * sc_iface; /* interface */ 86 int sc_iface_no; 87 88 enum uftdi_type sc_type; 89 u_int sc_flags; 90 #define FLAGS_BAUDCLK_12M 0x00000001 91 #define FLAGS_ROUNDOFF_232A 0x00000002 92 #define FLAGS_BAUDBITS_HINDEX 0x00000004 93 u_int sc_hdrlen; 94 u_int sc_chiptype; 95 96 u_char sc_msr; 97 u_char sc_lsr; 98 99 device_t sc_subdev; 100 101 bool sc_dying; 102 103 u_int last_lcr; 104 }; 105 106 static void uftdi_get_status(void *, int, u_char *, u_char *); 107 static void uftdi_set(void *, int, int, int); 108 static int uftdi_param(void *, int, struct termios *); 109 static int uftdi_open(void *, int); 110 static void uftdi_read(void *, int, u_char **, uint32_t *); 111 static void uftdi_write(void *, int, u_char *, u_char *, uint32_t *); 112 static void uftdi_break(void *, int, int); 113 114 static const struct ucom_methods uftdi_methods = { 115 .ucom_get_status = uftdi_get_status, 116 .ucom_set = uftdi_set, 117 .ucom_param = uftdi_param, 118 .ucom_open = uftdi_open, 119 .ucom_read = uftdi_read, 120 .ucom_write = uftdi_write, 121 }; 122 123 /* 124 * The devices default to UFTDI_TYPE_8U232AM. 125 * Remember to update uftdi_attach() if it should be UFTDI_TYPE_SIO instead 126 */ 127 static const struct usb_devno uftdi_devs[] = { 128 { USB_VENDOR_BBELECTRONICS, USB_PRODUCT_BBELECTRONICS_USOTL4 }, 129 { USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_TWIST }, 130 { USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_SAMBA }, 131 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_230X }, 132 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232H }, 133 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232RL }, 134 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_2232C }, 135 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_4232H }, 136 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U100AX }, 137 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM }, 138 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_KW }, 139 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_YS }, 140 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y6 }, 141 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y8 }, 142 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_IC }, 143 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_DB9 }, 144 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_RS232 }, 145 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y9 }, 146 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_COASTAL_TNCX }, 147 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_485_MINI }, 148 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_NANO_485 }, 149 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20 }, 150 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK202_24_USB }, 151 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK204_24_USB }, 152 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX200_USB }, 153 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX4_MX5_USB }, 154 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_631 }, 155 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_632 }, 156 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_633 }, 157 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_634 }, 158 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_635 }, 159 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_OPENRD_JTAGKEY }, 160 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_BEAGLEBONE }, 161 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MAXSTREAM_PKG_U }, 162 { USB_VENDOR_xxFTDI, USB_PRODUCT_xxFTDI_SHEEVAPLUG_JTAG }, 163 { USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_VALUECAN }, 164 { USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_NEOVI }, 165 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_PCOPRS1 }, 166 { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60F }, 167 { USB_VENDOR_RTSYS, USB_PRODUCT_RTSYS_CT57A }, 168 { USB_VENDOR_RTSYS, USB_PRODUCT_RTSYS_RTS03 }, 169 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_USBSERIAL }, 170 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P1 }, 171 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P2 }, 172 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P3 }, 173 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P4 }, 174 { USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_US2308 }, 175 { USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK }, 176 { USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK_DUO }, 177 }; 178 #define uftdi_lookup(v, p) usb_lookup(uftdi_devs, v, p) 179 180 static int uftdi_match(device_t, cfdata_t, void *); 181 static void uftdi_attach(device_t, device_t, void *); 182 static void uftdi_childdet(device_t, device_t); 183 static int uftdi_detach(device_t, int); 184 185 CFATTACH_DECL2_NEW(uftdi, sizeof(struct uftdi_softc), uftdi_match, 186 uftdi_attach, uftdi_detach, NULL, NULL, uftdi_childdet); 187 188 struct uftdi_match_quirk_entry { 189 uint16_t vendor_id; 190 uint16_t product_id; 191 int iface_no; 192 const char * vendor_str; 193 const char * product_str; 194 int match_ret; 195 }; 196 197 static const struct uftdi_match_quirk_entry uftdi_match_quirks[] = { 198 /* 199 * The Tigard board (https://github.com/tigard-tools/tigard) 200 * has two interfaces, one of which is meant to act as a 201 * regular USB serial port (interface 0), the other of which 202 * is meant for other protocols (SWD, JTAG, etc.). We must 203 * reject interface 1 so that ugenif matches, thus allowing 204 * full user-space control of that port. 205 */ 206 { 207 .vendor_id = USB_VENDOR_FTDI, 208 .product_id = USB_PRODUCT_FTDI_SERIAL_2232C, 209 .iface_no = 1, 210 .vendor_str = "SecuringHardware.com", 211 .product_str = "Tigard V1.1", 212 .match_ret = UMATCH_NONE, 213 } 214 }; 215 216 static int 217 uftdi_quirk_match(struct usbif_attach_arg *uiaa, int rv) 218 { 219 struct usbd_device *dev = uiaa->uiaa_device; 220 const struct uftdi_match_quirk_entry *q; 221 int i; 222 223 for (i = 0; i < __arraycount(uftdi_match_quirks); i++) { 224 q = &uftdi_match_quirks[i]; 225 if (uiaa->uiaa_vendor != q->vendor_id || 226 uiaa->uiaa_product != q->product_id || 227 uiaa->uiaa_ifaceno != q->iface_no) { 228 continue; 229 } 230 if (q->vendor_str != NULL && 231 (dev->ud_vendor == NULL || 232 strcmp(dev->ud_vendor, q->vendor_str) != 0)) { 233 continue; 234 } 235 if (q->product_str != NULL && 236 (dev->ud_product == NULL || 237 strcmp(dev->ud_product, q->product_str) != 0)) { 238 continue; 239 } 240 /* 241 * Got a match! 242 */ 243 rv = q->match_ret; 244 break; 245 } 246 return rv; 247 } 248 249 static int 250 uftdi_match(device_t parent, cfdata_t match, void *aux) 251 { 252 struct usbif_attach_arg *uiaa = aux; 253 int rv; 254 255 DPRINTFN(20,("uftdi: vendor=%#x, product=%#x\n", 256 uiaa->uiaa_vendor, uiaa->uiaa_product)); 257 258 if (uiaa->uiaa_configno != UFTDI_CONFIG_NO) 259 return UMATCH_NONE; 260 261 rv = uftdi_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL ? 262 UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE; 263 if (rv != UMATCH_NONE) { 264 rv = uftdi_quirk_match(uiaa, rv); 265 } 266 return rv; 267 } 268 269 static void 270 uftdi_attach(device_t parent, device_t self, void *aux) 271 { 272 struct uftdi_softc *sc = device_private(self); 273 struct usbif_attach_arg *uiaa = aux; 274 struct usbd_device *dev = uiaa->uiaa_device; 275 struct usbd_interface *iface = uiaa->uiaa_iface; 276 usb_device_descriptor_t *ddesc; 277 usb_interface_descriptor_t *id; 278 usb_endpoint_descriptor_t *ed; 279 char *devinfop; 280 int i; 281 struct ucom_attach_args ucaa; 282 283 DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc)); 284 285 aprint_naive("\n"); 286 aprint_normal("\n"); 287 288 devinfop = usbd_devinfo_alloc(dev, 0); 289 aprint_normal_dev(self, "%s\n", devinfop); 290 usbd_devinfo_free(devinfop); 291 292 sc->sc_dev = self; 293 sc->sc_udev = dev; 294 sc->sc_dying = false; 295 sc->sc_iface_no = uiaa->uiaa_ifaceno; 296 sc->sc_type = UFTDI_TYPE_8U232AM; /* most devices are post-8U232AM */ 297 sc->sc_hdrlen = 0; 298 299 ddesc = usbd_get_device_descriptor(dev); 300 sc->sc_chiptype = UGETW(ddesc->bcdDevice); 301 302 switch (sc->sc_chiptype) { 303 case 0x0200: 304 if (ddesc->iSerialNumber != 0) 305 sc->sc_flags |= FLAGS_ROUNDOFF_232A; 306 ucaa.ucaa_portno = 0; 307 break; 308 case 0x0400: 309 ucaa.ucaa_portno = 0; 310 break; 311 case 0x0500: 312 sc->sc_flags |= FLAGS_BAUDBITS_HINDEX; 313 ucaa.ucaa_portno = FTDI_PIT_SIOA + sc->sc_iface_no; 314 break; 315 case 0x0600: 316 ucaa.ucaa_portno = 0; 317 break; 318 case 0x0700: 319 case 0x0800: 320 case 0x0900: 321 sc->sc_flags |= FLAGS_BAUDCLK_12M; 322 sc->sc_flags |= FLAGS_BAUDBITS_HINDEX; 323 ucaa.ucaa_portno = FTDI_PIT_SIOA + sc->sc_iface_no; 324 break; 325 case 0x1000: 326 sc->sc_flags |= FLAGS_BAUDBITS_HINDEX; 327 ucaa.ucaa_portno = FTDI_PIT_SIOA + sc->sc_iface_no; 328 break; 329 default: 330 if (sc->sc_chiptype < 0x0200) { 331 sc->sc_type = UFTDI_TYPE_SIO; 332 sc->sc_hdrlen = 1; 333 } 334 ucaa.ucaa_portno = 0; 335 break; 336 } 337 338 id = usbd_get_interface_descriptor(iface); 339 340 sc->sc_iface = iface; 341 342 ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1; 343 ucaa.ucaa_ibufsize = ucaa.ucaa_obufsize = 0; 344 for (i = 0; i < id->bNumEndpoints; i++) { 345 int addr, dir, attr; 346 ed = usbd_interface2endpoint_descriptor(iface, i); 347 if (ed == NULL) { 348 aprint_error_dev(self, 349 "could not read endpoint descriptor\n"); 350 goto bad; 351 } 352 353 addr = ed->bEndpointAddress; 354 dir = UE_GET_DIR(ed->bEndpointAddress); 355 attr = ed->bmAttributes & UE_XFERTYPE; 356 if (dir == UE_DIR_IN && attr == UE_BULK) { 357 ucaa.ucaa_bulkin = addr; 358 ucaa.ucaa_ibufsize = UGETW(ed->wMaxPacketSize); 359 if (ucaa.ucaa_ibufsize >= UFTDI_MAX_IBUFSIZE) 360 ucaa.ucaa_ibufsize = UFTDI_MAX_IBUFSIZE; 361 } else if (dir == UE_DIR_OUT && attr == UE_BULK) { 362 ucaa.ucaa_bulkout = addr; 363 ucaa.ucaa_obufsize = UGETW(ed->wMaxPacketSize) 364 - sc->sc_hdrlen; 365 if (ucaa.ucaa_obufsize >= UFTDI_MAX_OBUFSIZE) 366 ucaa.ucaa_obufsize = UFTDI_MAX_OBUFSIZE; 367 /* Limit length if we have a 6-bit header. */ 368 if ((sc->sc_hdrlen > 0) && 369 (ucaa.ucaa_obufsize > UFTDIOBUFSIZE)) 370 ucaa.ucaa_obufsize = UFTDIOBUFSIZE; 371 } else { 372 aprint_error_dev(self, "unexpected endpoint\n"); 373 goto bad; 374 } 375 } 376 if (ucaa.ucaa_bulkin == -1) { 377 aprint_error_dev(self, "Could not find data bulk in\n"); 378 goto bad; 379 } 380 if (ucaa.ucaa_bulkout == -1) { 381 aprint_error_dev(self, "Could not find data bulk out\n"); 382 goto bad; 383 } 384 385 /* ucaa_bulkin, ucaa_bulkout set above */ 386 if (ucaa.ucaa_ibufsize == 0) 387 ucaa.ucaa_ibufsize = UFTDIIBUFSIZE; 388 ucaa.ucaa_ibufsizepad = ucaa.ucaa_ibufsize; 389 if (ucaa.ucaa_obufsize == 0) 390 ucaa.ucaa_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen; 391 ucaa.ucaa_opkthdrlen = sc->sc_hdrlen; 392 ucaa.ucaa_device = dev; 393 ucaa.ucaa_iface = iface; 394 ucaa.ucaa_methods = &uftdi_methods; 395 ucaa.ucaa_arg = sc; 396 ucaa.ucaa_info = NULL; 397 398 DPRINTF(("uftdi: in=%#x out=%#x isize=%#x osize=%#x\n", 399 ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, 400 ucaa.ucaa_ibufsize, ucaa.ucaa_obufsize)); 401 sc->sc_subdev = config_found(self, &ucaa, ucomprint, 402 CFARGS(.submatch = ucomsubmatch)); 403 404 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 405 406 if (!pmf_device_register(self, NULL, NULL)) 407 aprint_error_dev(self, "couldn't establish power handler\n"); 408 409 return; 410 411 bad: 412 DPRINTF(("uftdi_attach: ATTACH ERROR\n")); 413 sc->sc_dying = true; 414 return; 415 } 416 417 static void 418 uftdi_childdet(device_t self, device_t child) 419 { 420 struct uftdi_softc *sc = device_private(self); 421 422 KASSERT(child == sc->sc_subdev); 423 sc->sc_subdev = NULL; 424 } 425 426 static int 427 uftdi_detach(device_t self, int flags) 428 { 429 struct uftdi_softc *sc = device_private(self); 430 int rv = 0; 431 432 DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags)); 433 434 sc->sc_dying = true; 435 436 if (sc->sc_subdev != NULL) { 437 rv = config_detach(sc->sc_subdev, flags); 438 sc->sc_subdev = NULL; 439 } 440 441 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 442 443 return rv; 444 } 445 446 static int 447 uftdi_open(void *vsc, int portno) 448 { 449 struct uftdi_softc *sc = vsc; 450 usb_device_request_t req; 451 usbd_status err; 452 struct termios t; 453 454 DPRINTF(("uftdi_open: sc=%p\n", sc)); 455 456 if (sc->sc_dying) 457 return EIO; 458 459 /* Perform a full reset on the device */ 460 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 461 req.bRequest = FTDI_SIO_RESET; 462 USETW(req.wValue, FTDI_SIO_RESET_SIO); 463 USETW(req.wIndex, portno); 464 USETW(req.wLength, 0); 465 err = usbd_do_request(sc->sc_udev, &req, NULL); 466 if (err) 467 return EIO; 468 469 /* Set 9600 baud, 2 stop bits, no parity, 8 bits */ 470 t.c_ospeed = 9600; 471 t.c_cflag = CSTOPB | CS8; 472 (void)uftdi_param(sc, portno, &t); 473 474 /* Turn on RTS/CTS flow control */ 475 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 476 req.bRequest = FTDI_SIO_SET_FLOW_CTRL; 477 USETW(req.wValue, 0); 478 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno); 479 USETW(req.wLength, 0); 480 err = usbd_do_request(sc->sc_udev, &req, NULL); 481 if (err) 482 return EIO; 483 484 return 0; 485 } 486 487 static void 488 uftdi_read(void *vsc, int portno, u_char **ptr, uint32_t *count) 489 { 490 struct uftdi_softc *sc = vsc; 491 u_char msr, lsr; 492 493 DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno, 494 *count)); 495 496 msr = FTDI_GET_MSR(*ptr); 497 lsr = FTDI_GET_LSR(*ptr); 498 499 #ifdef UFTDI_DEBUG 500 if (*count != 2) 501 DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]=" 502 "0x%02x\n", sc, portno, *count, (*ptr)[2])); 503 #endif 504 505 if (sc->sc_msr != msr || 506 (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) { 507 DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) " 508 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr, 509 lsr, sc->sc_lsr)); 510 sc->sc_msr = msr; 511 sc->sc_lsr = lsr; 512 ucom_status_change(device_private(sc->sc_subdev)); 513 } 514 515 /* Adjust buffer pointer to skip status prefix */ 516 *ptr += 2; 517 } 518 519 static void 520 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, uint32_t *count) 521 { 522 struct uftdi_softc *sc = vsc; 523 524 DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n", 525 vsc, portno, *count, from[0])); 526 527 /* Make length tag and copy data */ 528 if (sc->sc_hdrlen > 0) 529 *to = FTDI_OUT_TAG(*count, portno); 530 531 memcpy(to + sc->sc_hdrlen, from, *count); 532 *count += sc->sc_hdrlen; 533 } 534 535 static void 536 uftdi_set(void *vsc, int portno, int reg, int onoff) 537 { 538 struct uftdi_softc *sc = vsc; 539 usb_device_request_t req; 540 int ctl; 541 542 DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno, 543 reg, onoff)); 544 545 if (sc->sc_dying) 546 return; 547 548 switch (reg) { 549 case UCOM_SET_DTR: 550 ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW; 551 break; 552 case UCOM_SET_RTS: 553 ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW; 554 break; 555 case UCOM_SET_BREAK: 556 uftdi_break(sc, portno, onoff); 557 return; 558 default: 559 return; 560 } 561 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 562 req.bRequest = FTDI_SIO_MODEM_CTRL; 563 USETW(req.wValue, ctl); 564 USETW(req.wIndex, portno); 565 USETW(req.wLength, 0); 566 DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x " 567 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, 568 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); 569 (void)usbd_do_request(sc->sc_udev, &req, NULL); 570 } 571 572 /* 573 * Return true if the given speed is within operational tolerance of the target 574 * speed. FTDI recommends that the hardware speed be within 3% of nominal. 575 */ 576 static inline bool 577 uftdi_baud_within_tolerance(uint64_t speed, uint64_t target) 578 { 579 return ((speed >= (target * 100) / 103) && 580 (speed <= (target * 100) / 97)); 581 } 582 583 static int 584 uftdi_encode_baudrate(struct uftdi_softc *sc, int speed, int *rate, int *ratehi) 585 { 586 static const uint8_t encoded_fraction[8] = { 587 0, 3, 2, 4, 1, 5, 6, 7 588 }; 589 static const uint8_t roundoff_232a[16] = { 590 0, 1, 0, 1, 0, -1, 2, 1, 591 0, -1, -2, -3, 4, 3, 2, 1, 592 }; 593 uint32_t clk, divisor, fastclk_flag, frac, hwspeed; 594 595 /* 596 * If this chip has the fast clock capability and the speed is within 597 * range, use the 12MHz clock, otherwise the standard clock is 3MHz. 598 */ 599 if ((sc->sc_flags & FLAGS_BAUDCLK_12M) && speed >= 1200) { 600 clk = 12000000; 601 fastclk_flag = (1 << 17); 602 } else { 603 clk = 3000000; 604 fastclk_flag = 0; 605 } 606 607 /* 608 * Make sure the requested speed is reachable with the available clock 609 * and a 14-bit divisor. 610 */ 611 if (speed < (clk >> 14) || speed > clk) 612 return -1; 613 614 /* 615 * Calculate the divisor, initially yielding a fixed point number with a 616 * 4-bit (1/16ths) fraction, then round it to the nearest fraction the 617 * hardware can handle. When the integral part of the divisor is 618 * greater than one, the fractional part is in 1/8ths of the base clock. 619 * The FT8U232AM chips can handle only 0.125, 0.250, and 0.5 fractions. 620 * Later chips can handle all 1/8th fractions. 621 * 622 * If the integral part of the divisor is 1, a special rule applies: the 623 * fractional part can only be .0 or .5 (this is a limitation of the 624 * hardware). We handle this by truncating the fraction rather than 625 * rounding, because this only applies to the two fastest speeds the 626 * chip can achieve and rounding doesn't matter, either you've asked for 627 * that exact speed or you've asked for something the chip can't do. 628 * 629 * For the FT8U232AM chips, use a roundoff table to adjust the result 630 * to the nearest 1/8th fraction that is supported by the hardware, 631 * leaving a fixed-point number with a 3-bit fraction which exactly 632 * represents the math the hardware divider will do. For later-series 633 * chips that support all 8 fractional divisors, just round 16ths to 634 * 8ths by adding 1 and dividing by 2. 635 */ 636 divisor = (clk << 4) / speed; 637 if ((divisor & 0xf) == 1) 638 divisor &= 0xfffffff8; 639 else if (sc->sc_flags & FLAGS_ROUNDOFF_232A) 640 divisor += roundoff_232a[divisor & 0x0f]; 641 else 642 divisor += 1; /* Rounds odd 16ths up to next 8th. */ 643 divisor >>= 1; 644 645 /* 646 * Ensure the resulting hardware speed will be within operational 647 * tolerance (within 3% of nominal). 648 */ 649 hwspeed = (clk << 3) / divisor; 650 if (!uftdi_baud_within_tolerance(hwspeed, speed)) 651 return -1; 652 653 /* 654 * Re-pack the divisor into hardware format. The lower 14-bits hold the 655 * integral part, while the upper bits specify the fraction by indexing 656 * a table of fractions within the hardware which is laid out as: 657 * {0.0, 0.5, 0.25, 0.125, 0.325, 0.625, 0.725, 0.875} 658 * The A-series chips only have the first four table entries; the 659 * roundoff table logic above ensures that the fractional part for those 660 * chips will be one of the first four values. 661 * 662 * When the divisor is 1 a special encoding applies: 1.0 is encoded as 663 * 0.0, and 1.5 is encoded as 1.0. The rounding logic above has already 664 * ensured that the fraction is either .0 or .5 if the integral is 1. 665 */ 666 frac = divisor & 0x07; 667 divisor >>= 3; 668 if (divisor == 1) { 669 if (frac == 0) 670 divisor = 0; /* 1.0 becomes 0.0 */ 671 else 672 frac = 0; /* 1.5 becomes 1.0 */ 673 } 674 divisor |= (encoded_fraction[frac] << 14) | fastclk_flag; 675 676 *rate = (uint16_t)divisor; 677 *ratehi = (uint16_t)(divisor >> 16); 678 679 /* 680 * If this chip requires the baud bits to be in the high byte of the 681 * index word, move the bits up to that location. 682 */ 683 if (sc->sc_flags & FLAGS_BAUDBITS_HINDEX) 684 *ratehi <<= 8; 685 686 return 0; 687 } 688 689 static int 690 uftdi_param(void *vsc, int portno, struct termios *t) 691 { 692 struct uftdi_softc *sc = vsc; 693 usb_device_request_t req; 694 usbd_status err; 695 int rate, ratehi, rerr, data, flow; 696 697 DPRINTF(("uftdi_param: sc=%p\n", sc)); 698 699 if (sc->sc_dying) 700 return EIO; 701 702 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 703 req.bRequest = FTDI_SIO_SET_BITMODE; 704 USETW(req.wValue, FTDI_BITMODE_RESET << 8 | 0x00); 705 USETW(req.wIndex, portno); 706 USETW(req.wLength, 0); 707 err = usbd_do_request(sc->sc_udev, &req, NULL); 708 if (err) 709 return EIO; 710 711 switch (sc->sc_type) { 712 case UFTDI_TYPE_SIO: 713 switch (t->c_ospeed) { 714 case 300: rate = ftdi_sio_b300; break; 715 case 600: rate = ftdi_sio_b600; break; 716 case 1200: rate = ftdi_sio_b1200; break; 717 case 2400: rate = ftdi_sio_b2400; break; 718 case 4800: rate = ftdi_sio_b4800; break; 719 case 9600: rate = ftdi_sio_b9600; break; 720 case 19200: rate = ftdi_sio_b19200; break; 721 case 38400: rate = ftdi_sio_b38400; break; 722 case 57600: rate = ftdi_sio_b57600; break; 723 case 115200: rate = ftdi_sio_b115200; break; 724 default: 725 return EINVAL; 726 } 727 ratehi = 0; 728 break; 729 case UFTDI_TYPE_8U232AM: 730 rerr = uftdi_encode_baudrate(sc, t->c_ospeed, &rate, &ratehi); 731 if (rerr != 0) 732 return EINVAL; 733 break; 734 default: 735 return EINVAL; 736 } 737 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 738 req.bRequest = FTDI_SIO_SET_BAUD_RATE; 739 USETW(req.wValue, rate); 740 USETW(req.wIndex, portno | ratehi); 741 USETW(req.wLength, 0); 742 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x " 743 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, 744 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); 745 err = usbd_do_request(sc->sc_udev, &req, NULL); 746 if (err) 747 return EIO; 748 749 if (ISSET(t->c_cflag, CSTOPB)) 750 data = FTDI_SIO_SET_DATA_STOP_BITS_2; 751 else 752 data = FTDI_SIO_SET_DATA_STOP_BITS_1; 753 if (ISSET(t->c_cflag, PARENB)) { 754 if (ISSET(t->c_cflag, PARODD)) 755 data |= FTDI_SIO_SET_DATA_PARITY_ODD; 756 else 757 data |= FTDI_SIO_SET_DATA_PARITY_EVEN; 758 } else 759 data |= FTDI_SIO_SET_DATA_PARITY_NONE; 760 switch (ISSET(t->c_cflag, CSIZE)) { 761 case CS5: 762 data |= FTDI_SIO_SET_DATA_BITS(5); 763 break; 764 case CS6: 765 data |= FTDI_SIO_SET_DATA_BITS(6); 766 break; 767 case CS7: 768 data |= FTDI_SIO_SET_DATA_BITS(7); 769 break; 770 case CS8: 771 data |= FTDI_SIO_SET_DATA_BITS(8); 772 break; 773 } 774 sc->last_lcr = data; 775 776 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 777 req.bRequest = FTDI_SIO_SET_DATA; 778 USETW(req.wValue, data); 779 USETW(req.wIndex, portno); 780 USETW(req.wLength, 0); 781 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x " 782 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, 783 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); 784 err = usbd_do_request(sc->sc_udev, &req, NULL); 785 if (err) 786 return EIO; 787 788 if (ISSET(t->c_cflag, CRTSCTS)) { 789 flow = FTDI_SIO_RTS_CTS_HS; 790 USETW(req.wValue, 0); 791 } else if (ISSET(t->c_iflag, IXON) && ISSET(t->c_iflag, IXOFF)) { 792 flow = FTDI_SIO_XON_XOFF_HS; 793 USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]); 794 } else { 795 flow = FTDI_SIO_DISABLE_FLOW_CTRL; 796 USETW(req.wValue, 0); 797 } 798 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 799 req.bRequest = FTDI_SIO_SET_FLOW_CTRL; 800 USETW2(req.wIndex, flow, portno); 801 USETW(req.wLength, 0); 802 err = usbd_do_request(sc->sc_udev, &req, NULL); 803 if (err) 804 return EIO; 805 806 return 0; 807 } 808 809 static void 810 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr) 811 { 812 struct uftdi_softc *sc = vsc; 813 814 DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n", 815 sc->sc_msr, sc->sc_lsr)); 816 817 if (sc->sc_dying) 818 return; 819 820 *msr = sc->sc_msr; 821 *lsr = sc->sc_lsr; 822 } 823 824 static void 825 uftdi_break(void *vsc, int portno, int onoff) 826 { 827 struct uftdi_softc *sc = vsc; 828 usb_device_request_t req; 829 int data; 830 831 DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno, 832 onoff)); 833 834 if (onoff) { 835 data = sc->last_lcr | FTDI_SIO_SET_BREAK; 836 } else { 837 data = sc->last_lcr; 838 } 839 840 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 841 req.bRequest = FTDI_SIO_SET_DATA; 842 USETW(req.wValue, data); 843 USETW(req.wIndex, portno); 844 USETW(req.wLength, 0); 845 (void)usbd_do_request(sc->sc_udev, &req, NULL); 846 } 847