1 /* $NetBSD: uftdi.c,v 1.80 2024/10/08 20:40:10 lloyd 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.80 2024/10/08 20:40:10 lloyd 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_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US101 }, 130 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US159 }, 131 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US235 }, 132 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US257 }, 133 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US279_12 }, 134 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US279_34 }, 135 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US279_56 }, 136 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US279_78 }, 137 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US313 }, 138 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US320 }, 139 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US324 }, 140 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US346_12 }, 141 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US346_34 }, 142 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US701_12 }, 143 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US701_34 }, 144 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US842_12 }, 145 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US842_34 }, 146 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US842_56 }, 147 { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US842_78 }, 148 { USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_TWIST }, 149 { USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_SAMBA }, 150 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_230X }, 151 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232H }, 152 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232RL }, 153 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_2232C }, 154 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_4232H }, 155 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U100AX }, 156 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM }, 157 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_KW }, 158 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_YS }, 159 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y6 }, 160 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y8 }, 161 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_IC }, 162 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_DB9 }, 163 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_RS232 }, 164 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y9 }, 165 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_COASTAL_TNCX }, 166 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_485_MINI }, 167 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_NANO_485 }, 168 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20 }, 169 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK202_24_USB }, 170 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK204_24_USB }, 171 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX200_USB }, 172 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX4_MX5_USB }, 173 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_631 }, 174 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_632 }, 175 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_633 }, 176 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_634 }, 177 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_635 }, 178 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_OPENRD_JTAGKEY }, 179 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_BEAGLEBONE }, 180 { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MAXSTREAM_PKG_U }, 181 { USB_VENDOR_xxFTDI, USB_PRODUCT_xxFTDI_SHEEVAPLUG_JTAG }, 182 { USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_VALUECAN }, 183 { USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_NEOVI }, 184 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_PCOPRS1 }, 185 { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60F }, 186 { USB_VENDOR_RTSYS, USB_PRODUCT_RTSYS_CT57A }, 187 { USB_VENDOR_RTSYS, USB_PRODUCT_RTSYS_RTS03 }, 188 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_USBSERIAL }, 189 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P1 }, 190 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P2 }, 191 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P3 }, 192 { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P4 }, 193 { USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_US2308 }, 194 { USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK }, 195 { USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK_DUO }, 196 }; 197 #define uftdi_lookup(v, p) usb_lookup(uftdi_devs, v, p) 198 199 static int uftdi_match(device_t, cfdata_t, void *); 200 static void uftdi_attach(device_t, device_t, void *); 201 static void uftdi_childdet(device_t, device_t); 202 static int uftdi_detach(device_t, int); 203 204 CFATTACH_DECL2_NEW(uftdi, sizeof(struct uftdi_softc), uftdi_match, 205 uftdi_attach, uftdi_detach, NULL, NULL, uftdi_childdet); 206 207 struct uftdi_match_quirk_entry { 208 uint16_t vendor_id; 209 uint16_t product_id; 210 int iface_no; 211 const char * vendor_str; 212 const char * product_str; 213 int match_ret; 214 }; 215 216 static const struct uftdi_match_quirk_entry uftdi_match_quirks[] = { 217 /* 218 * The Tigard board (https://github.com/tigard-tools/tigard) 219 * has two interfaces, one of which is meant to act as a 220 * regular USB serial port (interface 0), the other of which 221 * is meant for other protocols (SWD, JTAG, etc.). We must 222 * reject interface 1 so that ugenif matches, thus allowing 223 * full user-space control of that port. 224 */ 225 { 226 .vendor_id = USB_VENDOR_FTDI, 227 .product_id = USB_PRODUCT_FTDI_SERIAL_2232C, 228 .iface_no = 1, 229 .vendor_str = "SecuringHardware.com", 230 .product_str = "Tigard V1.1", 231 .match_ret = UMATCH_NONE, 232 }, 233 /* 234 * The SiPEED Tang Nano 9K (and other SiPEED Tang FPGA development 235 * boards) have an FT2232 on-board, wired up only for JTAG. 236 */ 237 { 238 .vendor_id = USB_VENDOR_FTDI, 239 .product_id = USB_PRODUCT_FTDI_SERIAL_2232C, 240 .iface_no = -1, 241 .vendor_str = "SIPEED", 242 .product_str = "JTAG Debugger", 243 .match_ret = UMATCH_NONE, 244 }, 245 /* 246 * The iCEBreaker board (https://1bitsquared.com/products/icebreaker) 247 * has two interfaces, one of which is meant to act as a 248 * regular USB serial port (interface 1), the other of which 249 * is meant for other protocols. 250 */ 251 { 252 .vendor_id = USB_VENDOR_FTDI, 253 .product_id = USB_PRODUCT_FTDI_SERIAL_2232C, 254 .iface_no = 0, 255 .vendor_str = "1BitSquared", 256 .product_str = "iCEBreaker *", 257 .match_ret = UMATCH_NONE, 258 }, 259 }; 260 261 static int 262 uftdi_quirk_match(struct usbif_attach_arg *uiaa, int rv) 263 { 264 struct usbd_device *dev = uiaa->uiaa_device; 265 const struct uftdi_match_quirk_entry *q; 266 int i; 267 268 for (i = 0; i < __arraycount(uftdi_match_quirks); i++) { 269 q = &uftdi_match_quirks[i]; 270 if (uiaa->uiaa_vendor != q->vendor_id || 271 uiaa->uiaa_product != q->product_id || 272 (q->iface_no != -1 && uiaa->uiaa_ifaceno != q->iface_no)) { 273 continue; 274 } 275 if (q->vendor_str != NULL && 276 (dev->ud_vendor == NULL || 277 strcmp(dev->ud_vendor, q->vendor_str) != 0)) { 278 continue; 279 } 280 if (q->product_str != NULL && 281 (dev->ud_product == NULL || 282 pmatch(dev->ud_product, q->product_str, NULL) != 2)) { 283 continue; 284 } 285 /* 286 * Got a match! 287 */ 288 rv = q->match_ret; 289 break; 290 } 291 return rv; 292 } 293 294 static int 295 uftdi_match(device_t parent, cfdata_t match, void *aux) 296 { 297 struct usbif_attach_arg *uiaa = aux; 298 int rv; 299 300 DPRINTFN(20,("uftdi: vendor=%#x, product=%#x\n", 301 uiaa->uiaa_vendor, uiaa->uiaa_product)); 302 303 if (uiaa->uiaa_configno != UFTDI_CONFIG_NO) 304 return UMATCH_NONE; 305 306 rv = uftdi_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL ? 307 UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE; 308 if (rv != UMATCH_NONE) { 309 rv = uftdi_quirk_match(uiaa, rv); 310 } 311 return rv; 312 } 313 314 static void 315 uftdi_attach(device_t parent, device_t self, void *aux) 316 { 317 struct uftdi_softc *sc = device_private(self); 318 struct usbif_attach_arg *uiaa = aux; 319 struct usbd_device *dev = uiaa->uiaa_device; 320 struct usbd_interface *iface = uiaa->uiaa_iface; 321 usb_device_descriptor_t *ddesc; 322 usb_interface_descriptor_t *id; 323 usb_endpoint_descriptor_t *ed; 324 char *devinfop; 325 int i; 326 struct ucom_attach_args ucaa; 327 328 DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc)); 329 330 aprint_naive("\n"); 331 aprint_normal("\n"); 332 333 devinfop = usbd_devinfo_alloc(dev, 0); 334 aprint_normal_dev(self, "%s\n", devinfop); 335 usbd_devinfo_free(devinfop); 336 337 sc->sc_dev = self; 338 sc->sc_udev = dev; 339 sc->sc_dying = false; 340 sc->sc_iface_no = uiaa->uiaa_ifaceno; 341 sc->sc_type = UFTDI_TYPE_8U232AM; /* most devices are post-8U232AM */ 342 sc->sc_hdrlen = 0; 343 344 ddesc = usbd_get_device_descriptor(dev); 345 sc->sc_chiptype = UGETW(ddesc->bcdDevice); 346 347 switch (sc->sc_chiptype) { 348 case 0x0200: 349 if (ddesc->iSerialNumber != 0) 350 sc->sc_flags |= FLAGS_ROUNDOFF_232A; 351 ucaa.ucaa_portno = 0; 352 break; 353 case 0x0400: 354 ucaa.ucaa_portno = 0; 355 break; 356 case 0x0500: 357 sc->sc_flags |= FLAGS_BAUDBITS_HINDEX; 358 ucaa.ucaa_portno = FTDI_PIT_SIOA + sc->sc_iface_no; 359 break; 360 case 0x0600: 361 ucaa.ucaa_portno = 0; 362 break; 363 case 0x0700: 364 case 0x0800: 365 case 0x0900: 366 sc->sc_flags |= FLAGS_BAUDCLK_12M; 367 sc->sc_flags |= FLAGS_BAUDBITS_HINDEX; 368 ucaa.ucaa_portno = FTDI_PIT_SIOA + sc->sc_iface_no; 369 break; 370 case 0x1000: 371 sc->sc_flags |= FLAGS_BAUDBITS_HINDEX; 372 ucaa.ucaa_portno = FTDI_PIT_SIOA + sc->sc_iface_no; 373 break; 374 default: 375 if (sc->sc_chiptype < 0x0200) { 376 sc->sc_type = UFTDI_TYPE_SIO; 377 sc->sc_hdrlen = 1; 378 } 379 ucaa.ucaa_portno = 0; 380 break; 381 } 382 383 id = usbd_get_interface_descriptor(iface); 384 385 sc->sc_iface = iface; 386 387 ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1; 388 ucaa.ucaa_ibufsize = ucaa.ucaa_obufsize = 0; 389 for (i = 0; i < id->bNumEndpoints; i++) { 390 int addr, dir, attr; 391 ed = usbd_interface2endpoint_descriptor(iface, i); 392 if (ed == NULL) { 393 aprint_error_dev(self, 394 "could not read endpoint descriptor\n"); 395 goto bad; 396 } 397 398 addr = ed->bEndpointAddress; 399 dir = UE_GET_DIR(ed->bEndpointAddress); 400 attr = ed->bmAttributes & UE_XFERTYPE; 401 if (dir == UE_DIR_IN && attr == UE_BULK) { 402 ucaa.ucaa_bulkin = addr; 403 ucaa.ucaa_ibufsize = UGETW(ed->wMaxPacketSize); 404 if (ucaa.ucaa_ibufsize >= UFTDI_MAX_IBUFSIZE) 405 ucaa.ucaa_ibufsize = UFTDI_MAX_IBUFSIZE; 406 } else if (dir == UE_DIR_OUT && attr == UE_BULK) { 407 ucaa.ucaa_bulkout = addr; 408 ucaa.ucaa_obufsize = UGETW(ed->wMaxPacketSize) 409 - sc->sc_hdrlen; 410 if (ucaa.ucaa_obufsize >= UFTDI_MAX_OBUFSIZE) 411 ucaa.ucaa_obufsize = UFTDI_MAX_OBUFSIZE; 412 /* Limit length if we have a 6-bit header. */ 413 if ((sc->sc_hdrlen > 0) && 414 (ucaa.ucaa_obufsize > UFTDIOBUFSIZE)) 415 ucaa.ucaa_obufsize = UFTDIOBUFSIZE; 416 } else { 417 aprint_error_dev(self, "unexpected endpoint\n"); 418 goto bad; 419 } 420 } 421 if (ucaa.ucaa_bulkin == -1) { 422 aprint_error_dev(self, "Could not find data bulk in\n"); 423 goto bad; 424 } 425 if (ucaa.ucaa_bulkout == -1) { 426 aprint_error_dev(self, "Could not find data bulk out\n"); 427 goto bad; 428 } 429 430 /* ucaa_bulkin, ucaa_bulkout set above */ 431 if (ucaa.ucaa_ibufsize == 0) 432 ucaa.ucaa_ibufsize = UFTDIIBUFSIZE; 433 ucaa.ucaa_ibufsizepad = ucaa.ucaa_ibufsize; 434 if (ucaa.ucaa_obufsize == 0) 435 ucaa.ucaa_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen; 436 ucaa.ucaa_opkthdrlen = sc->sc_hdrlen; 437 ucaa.ucaa_device = dev; 438 ucaa.ucaa_iface = iface; 439 ucaa.ucaa_methods = &uftdi_methods; 440 ucaa.ucaa_arg = sc; 441 ucaa.ucaa_info = NULL; 442 443 DPRINTF(("uftdi: in=%#x out=%#x isize=%#x osize=%#x\n", 444 ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, 445 ucaa.ucaa_ibufsize, ucaa.ucaa_obufsize)); 446 sc->sc_subdev = config_found(self, &ucaa, ucomprint, 447 CFARGS(.submatch = ucomsubmatch)); 448 449 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 450 451 if (!pmf_device_register(self, NULL, NULL)) 452 aprint_error_dev(self, "couldn't establish power handler\n"); 453 454 return; 455 456 bad: 457 DPRINTF(("uftdi_attach: ATTACH ERROR\n")); 458 sc->sc_dying = true; 459 return; 460 } 461 462 static void 463 uftdi_childdet(device_t self, device_t child) 464 { 465 struct uftdi_softc *sc = device_private(self); 466 467 KASSERT(child == sc->sc_subdev); 468 sc->sc_subdev = NULL; 469 } 470 471 static int 472 uftdi_detach(device_t self, int flags) 473 { 474 struct uftdi_softc *sc = device_private(self); 475 int rv = 0; 476 477 DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags)); 478 479 sc->sc_dying = true; 480 481 if (sc->sc_subdev != NULL) { 482 rv = config_detach(sc->sc_subdev, flags); 483 sc->sc_subdev = NULL; 484 } 485 486 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 487 488 return rv; 489 } 490 491 static int 492 uftdi_open(void *vsc, int portno) 493 { 494 struct uftdi_softc *sc = vsc; 495 usb_device_request_t req; 496 usbd_status err; 497 struct termios t; 498 499 DPRINTF(("uftdi_open: sc=%p\n", sc)); 500 501 if (sc->sc_dying) 502 return EIO; 503 504 /* Perform a full reset on the device */ 505 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 506 req.bRequest = FTDI_SIO_RESET; 507 USETW(req.wValue, FTDI_SIO_RESET_SIO); 508 USETW(req.wIndex, portno); 509 USETW(req.wLength, 0); 510 err = usbd_do_request(sc->sc_udev, &req, NULL); 511 if (err) 512 return EIO; 513 514 /* Set 9600 baud, 2 stop bits, no parity, 8 bits */ 515 t.c_ospeed = 9600; 516 t.c_cflag = CSTOPB | CS8; 517 (void)uftdi_param(sc, portno, &t); 518 519 /* Turn on RTS/CTS flow control */ 520 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 521 req.bRequest = FTDI_SIO_SET_FLOW_CTRL; 522 USETW(req.wValue, 0); 523 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno); 524 USETW(req.wLength, 0); 525 err = usbd_do_request(sc->sc_udev, &req, NULL); 526 if (err) 527 return EIO; 528 529 return 0; 530 } 531 532 static void 533 uftdi_read(void *vsc, int portno, u_char **ptr, uint32_t *count) 534 { 535 struct uftdi_softc *sc = vsc; 536 u_char msr, lsr; 537 538 DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno, 539 *count)); 540 541 msr = FTDI_GET_MSR(*ptr); 542 lsr = FTDI_GET_LSR(*ptr); 543 544 #ifdef UFTDI_DEBUG 545 if (*count != 2) 546 DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]=" 547 "0x%02x\n", sc, portno, *count, (*ptr)[2])); 548 #endif 549 550 if (sc->sc_msr != msr || 551 (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) { 552 DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) " 553 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr, 554 lsr, sc->sc_lsr)); 555 sc->sc_msr = msr; 556 sc->sc_lsr = lsr; 557 ucom_status_change(device_private(sc->sc_subdev)); 558 } 559 560 /* Adjust buffer pointer to skip status prefix */ 561 *ptr += 2; 562 } 563 564 static void 565 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, uint32_t *count) 566 { 567 struct uftdi_softc *sc = vsc; 568 569 DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n", 570 vsc, portno, *count, from[0])); 571 572 /* Make length tag and copy data */ 573 if (sc->sc_hdrlen > 0) 574 *to = FTDI_OUT_TAG(*count, portno); 575 576 memcpy(to + sc->sc_hdrlen, from, *count); 577 *count += sc->sc_hdrlen; 578 } 579 580 static void 581 uftdi_set(void *vsc, int portno, int reg, int onoff) 582 { 583 struct uftdi_softc *sc = vsc; 584 usb_device_request_t req; 585 int ctl; 586 587 DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno, 588 reg, onoff)); 589 590 if (sc->sc_dying) 591 return; 592 593 switch (reg) { 594 case UCOM_SET_DTR: 595 ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW; 596 break; 597 case UCOM_SET_RTS: 598 ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW; 599 break; 600 case UCOM_SET_BREAK: 601 uftdi_break(sc, portno, onoff); 602 return; 603 default: 604 return; 605 } 606 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 607 req.bRequest = FTDI_SIO_MODEM_CTRL; 608 USETW(req.wValue, ctl); 609 USETW(req.wIndex, portno); 610 USETW(req.wLength, 0); 611 DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x " 612 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, 613 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); 614 (void)usbd_do_request(sc->sc_udev, &req, NULL); 615 } 616 617 /* 618 * Return true if the given speed is within operational tolerance of the target 619 * speed. FTDI recommends that the hardware speed be within 3% of nominal. 620 */ 621 static inline bool 622 uftdi_baud_within_tolerance(uint64_t speed, uint64_t target) 623 { 624 return ((speed >= (target * 100) / 103) && 625 (speed <= (target * 100) / 97)); 626 } 627 628 static int 629 uftdi_encode_baudrate(struct uftdi_softc *sc, int speed, int *rate, int *ratehi) 630 { 631 static const uint8_t encoded_fraction[8] = { 632 0, 3, 2, 4, 1, 5, 6, 7 633 }; 634 static const uint8_t roundoff_232a[16] = { 635 0, 1, 0, 1, 0, -1, 2, 1, 636 0, -1, -2, -3, 4, 3, 2, 1, 637 }; 638 uint32_t clk, divisor, fastclk_flag, frac, hwspeed; 639 640 /* 641 * If this chip has the fast clock capability and the speed is within 642 * range, use the 12MHz clock, otherwise the standard clock is 3MHz. 643 */ 644 if ((sc->sc_flags & FLAGS_BAUDCLK_12M) && speed >= 1200) { 645 clk = 12000000; 646 fastclk_flag = (1 << 17); 647 } else { 648 clk = 3000000; 649 fastclk_flag = 0; 650 } 651 652 /* 653 * Make sure the requested speed is reachable with the available clock 654 * and a 14-bit divisor. 655 */ 656 if (speed < (clk >> 14) || speed > clk) 657 return -1; 658 659 /* 660 * Calculate the divisor, initially yielding a fixed point number with a 661 * 4-bit (1/16ths) fraction, then round it to the nearest fraction the 662 * hardware can handle. When the integral part of the divisor is 663 * greater than one, the fractional part is in 1/8ths of the base clock. 664 * The FT8U232AM chips can handle only 0.125, 0.250, and 0.5 fractions. 665 * Later chips can handle all 1/8th fractions. 666 * 667 * If the integral part of the divisor is 1, a special rule applies: the 668 * fractional part can only be .0 or .5 (this is a limitation of the 669 * hardware). We handle this by truncating the fraction rather than 670 * rounding, because this only applies to the two fastest speeds the 671 * chip can achieve and rounding doesn't matter, either you've asked for 672 * that exact speed or you've asked for something the chip can't do. 673 * 674 * For the FT8U232AM chips, use a roundoff table to adjust the result 675 * to the nearest 1/8th fraction that is supported by the hardware, 676 * leaving a fixed-point number with a 3-bit fraction which exactly 677 * represents the math the hardware divider will do. For later-series 678 * chips that support all 8 fractional divisors, just round 16ths to 679 * 8ths by adding 1 and dividing by 2. 680 */ 681 divisor = (clk << 4) / speed; 682 if ((divisor & 0xf) == 1) 683 divisor &= 0xfffffff8; 684 else if (sc->sc_flags & FLAGS_ROUNDOFF_232A) 685 divisor += roundoff_232a[divisor & 0x0f]; 686 else 687 divisor += 1; /* Rounds odd 16ths up to next 8th. */ 688 divisor >>= 1; 689 690 /* 691 * Ensure the resulting hardware speed will be within operational 692 * tolerance (within 3% of nominal). 693 */ 694 hwspeed = (clk << 3) / divisor; 695 if (!uftdi_baud_within_tolerance(hwspeed, speed)) 696 return -1; 697 698 /* 699 * Re-pack the divisor into hardware format. The lower 14-bits hold the 700 * integral part, while the upper bits specify the fraction by indexing 701 * a table of fractions within the hardware which is laid out as: 702 * {0.0, 0.5, 0.25, 0.125, 0.325, 0.625, 0.725, 0.875} 703 * The A-series chips only have the first four table entries; the 704 * roundoff table logic above ensures that the fractional part for those 705 * chips will be one of the first four values. 706 * 707 * When the divisor is 1 a special encoding applies: 1.0 is encoded as 708 * 0.0, and 1.5 is encoded as 1.0. The rounding logic above has already 709 * ensured that the fraction is either .0 or .5 if the integral is 1. 710 */ 711 frac = divisor & 0x07; 712 divisor >>= 3; 713 if (divisor == 1) { 714 if (frac == 0) 715 divisor = 0; /* 1.0 becomes 0.0 */ 716 else 717 frac = 0; /* 1.5 becomes 1.0 */ 718 } 719 divisor |= (encoded_fraction[frac] << 14) | fastclk_flag; 720 721 *rate = (uint16_t)divisor; 722 *ratehi = (uint16_t)(divisor >> 16); 723 724 /* 725 * If this chip requires the baud bits to be in the high byte of the 726 * index word, move the bits up to that location. 727 */ 728 if (sc->sc_flags & FLAGS_BAUDBITS_HINDEX) 729 *ratehi <<= 8; 730 731 return 0; 732 } 733 734 static int 735 uftdi_param(void *vsc, int portno, struct termios *t) 736 { 737 struct uftdi_softc *sc = vsc; 738 usb_device_request_t req; 739 usbd_status err; 740 int rate, ratehi, rerr, data, flow; 741 742 DPRINTF(("uftdi_param: sc=%p\n", sc)); 743 744 if (sc->sc_dying) 745 return EIO; 746 747 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 748 req.bRequest = FTDI_SIO_SET_BITMODE; 749 USETW(req.wValue, FTDI_BITMODE_RESET << 8 | 0x00); 750 USETW(req.wIndex, portno); 751 USETW(req.wLength, 0); 752 err = usbd_do_request(sc->sc_udev, &req, NULL); 753 if (err) 754 return EIO; 755 756 switch (sc->sc_type) { 757 case UFTDI_TYPE_SIO: 758 switch (t->c_ospeed) { 759 case 300: rate = ftdi_sio_b300; break; 760 case 600: rate = ftdi_sio_b600; break; 761 case 1200: rate = ftdi_sio_b1200; break; 762 case 2400: rate = ftdi_sio_b2400; break; 763 case 4800: rate = ftdi_sio_b4800; break; 764 case 9600: rate = ftdi_sio_b9600; break; 765 case 19200: rate = ftdi_sio_b19200; break; 766 case 38400: rate = ftdi_sio_b38400; break; 767 case 57600: rate = ftdi_sio_b57600; break; 768 case 115200: rate = ftdi_sio_b115200; break; 769 default: 770 return EINVAL; 771 } 772 ratehi = 0; 773 break; 774 case UFTDI_TYPE_8U232AM: 775 rerr = uftdi_encode_baudrate(sc, t->c_ospeed, &rate, &ratehi); 776 if (rerr != 0) 777 return EINVAL; 778 break; 779 default: 780 return EINVAL; 781 } 782 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 783 req.bRequest = FTDI_SIO_SET_BAUD_RATE; 784 USETW(req.wValue, rate); 785 USETW(req.wIndex, portno | ratehi); 786 USETW(req.wLength, 0); 787 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x " 788 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, 789 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); 790 err = usbd_do_request(sc->sc_udev, &req, NULL); 791 if (err) 792 return EIO; 793 794 if (ISSET(t->c_cflag, CSTOPB)) 795 data = FTDI_SIO_SET_DATA_STOP_BITS_2; 796 else 797 data = FTDI_SIO_SET_DATA_STOP_BITS_1; 798 if (ISSET(t->c_cflag, PARENB)) { 799 if (ISSET(t->c_cflag, PARODD)) 800 data |= FTDI_SIO_SET_DATA_PARITY_ODD; 801 else 802 data |= FTDI_SIO_SET_DATA_PARITY_EVEN; 803 } else 804 data |= FTDI_SIO_SET_DATA_PARITY_NONE; 805 switch (ISSET(t->c_cflag, CSIZE)) { 806 case CS5: 807 data |= FTDI_SIO_SET_DATA_BITS(5); 808 break; 809 case CS6: 810 data |= FTDI_SIO_SET_DATA_BITS(6); 811 break; 812 case CS7: 813 data |= FTDI_SIO_SET_DATA_BITS(7); 814 break; 815 case CS8: 816 data |= FTDI_SIO_SET_DATA_BITS(8); 817 break; 818 } 819 sc->last_lcr = data; 820 821 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 822 req.bRequest = FTDI_SIO_SET_DATA; 823 USETW(req.wValue, data); 824 USETW(req.wIndex, portno); 825 USETW(req.wLength, 0); 826 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x " 827 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, 828 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); 829 err = usbd_do_request(sc->sc_udev, &req, NULL); 830 if (err) 831 return EIO; 832 833 if (ISSET(t->c_cflag, CRTSCTS)) { 834 flow = FTDI_SIO_RTS_CTS_HS; 835 USETW(req.wValue, 0); 836 } else if (ISSET(t->c_iflag, IXON) && ISSET(t->c_iflag, IXOFF)) { 837 flow = FTDI_SIO_XON_XOFF_HS; 838 USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]); 839 } else { 840 flow = FTDI_SIO_DISABLE_FLOW_CTRL; 841 USETW(req.wValue, 0); 842 } 843 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 844 req.bRequest = FTDI_SIO_SET_FLOW_CTRL; 845 USETW2(req.wIndex, flow, portno); 846 USETW(req.wLength, 0); 847 err = usbd_do_request(sc->sc_udev, &req, NULL); 848 if (err) 849 return EIO; 850 851 return 0; 852 } 853 854 static void 855 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr) 856 { 857 struct uftdi_softc *sc = vsc; 858 859 DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n", 860 sc->sc_msr, sc->sc_lsr)); 861 862 if (sc->sc_dying) 863 return; 864 865 *msr = sc->sc_msr; 866 *lsr = sc->sc_lsr; 867 } 868 869 static void 870 uftdi_break(void *vsc, int portno, int onoff) 871 { 872 struct uftdi_softc *sc = vsc; 873 usb_device_request_t req; 874 int data; 875 876 DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno, 877 onoff)); 878 879 if (onoff) { 880 data = sc->last_lcr | FTDI_SIO_SET_BREAK; 881 } else { 882 data = sc->last_lcr; 883 } 884 885 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 886 req.bRequest = FTDI_SIO_SET_DATA; 887 USETW(req.wValue, data); 888 USETW(req.wIndex, portno); 889 USETW(req.wLength, 0); 890 (void)usbd_do_request(sc->sc_udev, &req, NULL); 891 } 892