1 /* $NetBSD: uftdi.c,v 1.13 2002/09/23 05:51:23 simonb 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 /* 43 * NOTE: all function names beginning like "uftdi_cfg_" can only 44 * be called from within the config thread function ! 45 */ 46 47 /* 48 * FTDI FT8U100AX serial adapter driver 49 */ 50 51 #include <sys/stdint.h> 52 #include <sys/stddef.h> 53 #include <sys/param.h> 54 #include <sys/queue.h> 55 #include <sys/types.h> 56 #include <sys/systm.h> 57 #include <sys/kernel.h> 58 #include <sys/bus.h> 59 #include <sys/linker_set.h> 60 #include <sys/module.h> 61 #include <sys/lock.h> 62 #include <sys/mutex.h> 63 #include <sys/condvar.h> 64 #include <sys/sysctl.h> 65 #include <sys/sx.h> 66 #include <sys/unistd.h> 67 #include <sys/callout.h> 68 #include <sys/malloc.h> 69 #include <sys/priv.h> 70 71 #include <dev/usb/usb.h> 72 #include <dev/usb/usbdi.h> 73 #include <dev/usb/usbdi_util.h> 74 #include "usbdevs.h" 75 76 #define USB_DEBUG_VAR uftdi_debug 77 #include <dev/usb/usb_debug.h> 78 #include <dev/usb/usb_process.h> 79 80 #include <dev/usb/serial/usb_serial.h> 81 #include <dev/usb/serial/uftdi_reg.h> 82 83 #if USB_DEBUG 84 static int uftdi_debug = 0; 85 86 SYSCTL_NODE(_hw_usb, OID_AUTO, uftdi, CTLFLAG_RW, 0, "USB uftdi"); 87 SYSCTL_INT(_hw_usb_uftdi, OID_AUTO, debug, CTLFLAG_RW, 88 &uftdi_debug, 0, "Debug level"); 89 #endif 90 91 #define UFTDI_CONFIG_INDEX 0 92 #define UFTDI_IFACE_INDEX 0 93 94 #define UFTDI_IBUFSIZE 64 /* bytes, maximum number of bytes per 95 * frame */ 96 #define UFTDI_OBUFSIZE 64 /* bytes, cannot be increased due to 97 * do size encoding */ 98 99 enum { 100 UFTDI_BULK_DT_WR, 101 UFTDI_BULK_DT_RD, 102 UFTDI_N_TRANSFER, 103 }; 104 105 struct uftdi_softc { 106 struct ucom_super_softc sc_super_ucom; 107 struct ucom_softc sc_ucom; 108 109 struct usb_device *sc_udev; 110 struct usb_xfer *sc_xfer[UFTDI_N_TRANSFER]; 111 device_t sc_dev; 112 struct mtx sc_mtx; 113 114 uint32_t sc_unit; 115 enum uftdi_type sc_type; 116 117 uint16_t sc_last_lcr; 118 119 uint8_t sc_iface_index; 120 uint8_t sc_hdrlen; 121 uint8_t sc_msr; 122 uint8_t sc_lsr; 123 124 uint8_t sc_name[16]; 125 }; 126 127 struct uftdi_param_config { 128 uint16_t rate; 129 uint16_t lcr; 130 uint8_t v_start; 131 uint8_t v_stop; 132 uint8_t v_flow; 133 }; 134 135 /* prototypes */ 136 137 static device_probe_t uftdi_probe; 138 static device_attach_t uftdi_attach; 139 static device_detach_t uftdi_detach; 140 141 static usb_callback_t uftdi_write_callback; 142 static usb_callback_t uftdi_read_callback; 143 144 static void uftdi_cfg_open(struct ucom_softc *); 145 static void uftdi_cfg_set_dtr(struct ucom_softc *, uint8_t); 146 static void uftdi_cfg_set_rts(struct ucom_softc *, uint8_t); 147 static void uftdi_cfg_set_break(struct ucom_softc *, uint8_t); 148 static int uftdi_set_parm_soft(struct termios *, 149 struct uftdi_param_config *, uint8_t); 150 static int uftdi_pre_param(struct ucom_softc *, struct termios *); 151 static void uftdi_cfg_param(struct ucom_softc *, struct termios *); 152 static void uftdi_cfg_get_status(struct ucom_softc *, uint8_t *, 153 uint8_t *); 154 static void uftdi_start_read(struct ucom_softc *); 155 static void uftdi_stop_read(struct ucom_softc *); 156 static void uftdi_start_write(struct ucom_softc *); 157 static void uftdi_stop_write(struct ucom_softc *); 158 static uint8_t uftdi_8u232am_getrate(uint32_t, uint16_t *); 159 static void uftdi_poll(struct ucom_softc *ucom); 160 161 static const struct usb_config uftdi_config[UFTDI_N_TRANSFER] = { 162 163 [UFTDI_BULK_DT_WR] = { 164 .type = UE_BULK, 165 .endpoint = UE_ADDR_ANY, 166 .direction = UE_DIR_OUT, 167 .bufsize = UFTDI_OBUFSIZE, 168 .flags = {.pipe_bof = 1,}, 169 .callback = &uftdi_write_callback, 170 }, 171 172 [UFTDI_BULK_DT_RD] = { 173 .type = UE_BULK, 174 .endpoint = UE_ADDR_ANY, 175 .direction = UE_DIR_IN, 176 .bufsize = UFTDI_IBUFSIZE, 177 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 178 .callback = &uftdi_read_callback, 179 }, 180 }; 181 182 static const struct ucom_callback uftdi_callback = { 183 .ucom_cfg_get_status = &uftdi_cfg_get_status, 184 .ucom_cfg_set_dtr = &uftdi_cfg_set_dtr, 185 .ucom_cfg_set_rts = &uftdi_cfg_set_rts, 186 .ucom_cfg_set_break = &uftdi_cfg_set_break, 187 .ucom_cfg_param = &uftdi_cfg_param, 188 .ucom_cfg_open = &uftdi_cfg_open, 189 .ucom_pre_param = &uftdi_pre_param, 190 .ucom_start_read = &uftdi_start_read, 191 .ucom_stop_read = &uftdi_stop_read, 192 .ucom_start_write = &uftdi_start_write, 193 .ucom_stop_write = &uftdi_stop_write, 194 .ucom_poll = &uftdi_poll, 195 }; 196 197 static device_method_t uftdi_methods[] = { 198 /* Device interface */ 199 DEVMETHOD(device_probe, uftdi_probe), 200 DEVMETHOD(device_attach, uftdi_attach), 201 DEVMETHOD(device_detach, uftdi_detach), 202 203 {0, 0} 204 }; 205 206 static devclass_t uftdi_devclass; 207 208 static driver_t uftdi_driver = { 209 .name = "uftdi", 210 .methods = uftdi_methods, 211 .size = sizeof(struct uftdi_softc), 212 }; 213 214 DRIVER_MODULE(uftdi, uhub, uftdi_driver, uftdi_devclass, NULL, 0); 215 MODULE_DEPEND(uftdi, ucom, 1, 1, 1); 216 MODULE_DEPEND(uftdi, usb, 1, 1, 1); 217 218 static struct usb_device_id uftdi_devs[] = { 219 #define UFTDI_DEV(v,p,t) \ 220 { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, UFTDI_TYPE_##t) } 221 UFTDI_DEV(ATMEL, STK541, 8U232AM), 222 UFTDI_DEV(DRESDENELEKTRONIK, SENSORTERMINALBOARD, 8U232AM), 223 UFTDI_DEV(DRESDENELEKTRONIK, WIRELESSHANDHELDTERMINAL, 8U232AM), 224 UFTDI_DEV(FTDI, SERIAL_8U100AX, SIO), 225 UFTDI_DEV(FTDI, SERIAL_2232C, 8U232AM), 226 UFTDI_DEV(FTDI, SERIAL_2232D, 8U232AM), 227 UFTDI_DEV(FTDI, SERIAL_4232H, 8U232AM), 228 UFTDI_DEV(FTDI, SERIAL_8U232AM, 8U232AM), 229 UFTDI_DEV(FTDI, SERIAL_8U232AM4, 8U232AM), 230 UFTDI_DEV(FTDI, SEMC_DSS20, 8U232AM), 231 UFTDI_DEV(FTDI, CFA_631, 8U232AM), 232 UFTDI_DEV(FTDI, CFA_632, 8U232AM), 233 UFTDI_DEV(FTDI, CFA_633, 8U232AM), 234 UFTDI_DEV(FTDI, CFA_634, 8U232AM), 235 UFTDI_DEV(FTDI, CFA_635, 8U232AM), 236 UFTDI_DEV(FTDI, USBSERIAL, 8U232AM), 237 UFTDI_DEV(FTDI, MX2_3, 8U232AM), 238 UFTDI_DEV(FTDI, MX4_5, 8U232AM), 239 UFTDI_DEV(FTDI, LK202, 8U232AM), 240 UFTDI_DEV(FTDI, LK204, 8U232AM), 241 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13M, 8U232AM), 242 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13S, 8U232AM), 243 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13U, 8U232AM), 244 UFTDI_DEV(FTDI, EISCOU, 8U232AM), 245 UFTDI_DEV(FTDI, UOPTBR, 8U232AM), 246 UFTDI_DEV(FTDI, EMCU2D, 8U232AM), 247 UFTDI_DEV(FTDI, PCMSFU, 8U232AM), 248 UFTDI_DEV(FTDI, EMCU2H, 8U232AM), 249 UFTDI_DEV(FTDI, MAXSTREAM, 8U232AM), 250 UFTDI_DEV(FTDI, CTI_USB_NANO_485, 8U232AM), 251 UFTDI_DEV(FTDI, CTI_USB_MINI_485, 8U232AM), 252 UFTDI_DEV(SIIG2, US2308, 8U232AM), 253 UFTDI_DEV(INTREPIDCS, VALUECAN, 8U232AM), 254 UFTDI_DEV(INTREPIDCS, NEOVI, 8U232AM), 255 UFTDI_DEV(BBELECTRONICS, USOTL4, 8U232AM), 256 UFTDI_DEV(MARVELL, SHEEVAPLUG, 8U232AM), 257 UFTDI_DEV(MELCO, PCOPRS1, 8U232AM), 258 UFTDI_DEV(RATOC, REXUSB60F, 8U232AM), 259 #undef UFTDI_DEV 260 }; 261 262 static int 263 uftdi_probe(device_t dev) 264 { 265 struct usb_attach_arg *uaa = device_get_ivars(dev); 266 267 if (uaa->usb_mode != USB_MODE_HOST) { 268 return (ENXIO); 269 } 270 if (uaa->info.bConfigIndex != UFTDI_CONFIG_INDEX) { 271 return (ENXIO); 272 } 273 /* attach to all present interfaces */ 274 275 return (usbd_lookup_id_by_uaa(uftdi_devs, sizeof(uftdi_devs), uaa)); 276 } 277 278 static int 279 uftdi_attach(device_t dev) 280 { 281 struct usb_attach_arg *uaa = device_get_ivars(dev); 282 struct uftdi_softc *sc = device_get_softc(dev); 283 int error; 284 285 sc->sc_udev = uaa->device; 286 sc->sc_dev = dev; 287 sc->sc_unit = device_get_unit(dev); 288 289 device_set_usb_desc(dev); 290 mtx_init(&sc->sc_mtx, "uftdi", NULL, MTX_DEF); 291 292 snprintf(sc->sc_name, sizeof(sc->sc_name), 293 "%s", device_get_nameunit(dev)); 294 295 DPRINTF("\n"); 296 297 sc->sc_iface_index = uaa->info.bIfaceIndex; 298 sc->sc_type = USB_GET_DRIVER_INFO(uaa); 299 300 switch (sc->sc_type) { 301 case UFTDI_TYPE_SIO: 302 sc->sc_hdrlen = 1; 303 break; 304 case UFTDI_TYPE_8U232AM: 305 default: 306 sc->sc_hdrlen = 0; 307 break; 308 } 309 310 error = usbd_transfer_setup(uaa->device, 311 &sc->sc_iface_index, sc->sc_xfer, uftdi_config, 312 UFTDI_N_TRANSFER, sc, &sc->sc_mtx); 313 314 if (error) { 315 device_printf(dev, "allocating USB " 316 "transfers failed\n"); 317 goto detach; 318 } 319 sc->sc_ucom.sc_portno = FTDI_PIT_SIOA + uaa->info.bIfaceNum; 320 321 /* clear stall at first run */ 322 mtx_lock(&sc->sc_mtx); 323 usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_WR]); 324 usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_RD]); 325 mtx_unlock(&sc->sc_mtx); 326 327 /* set a valid "lcr" value */ 328 329 sc->sc_last_lcr = 330 (FTDI_SIO_SET_DATA_STOP_BITS_2 | 331 FTDI_SIO_SET_DATA_PARITY_NONE | 332 FTDI_SIO_SET_DATA_BITS(8)); 333 334 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 335 &uftdi_callback, &sc->sc_mtx); 336 if (error) { 337 goto detach; 338 } 339 return (0); /* success */ 340 341 detach: 342 uftdi_detach(dev); 343 return (ENXIO); 344 } 345 346 static int 347 uftdi_detach(device_t dev) 348 { 349 struct uftdi_softc *sc = device_get_softc(dev); 350 351 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1); 352 usbd_transfer_unsetup(sc->sc_xfer, UFTDI_N_TRANSFER); 353 mtx_destroy(&sc->sc_mtx); 354 355 return (0); 356 } 357 358 static void 359 uftdi_cfg_open(struct ucom_softc *ucom) 360 { 361 struct uftdi_softc *sc = ucom->sc_parent; 362 uint16_t wIndex = ucom->sc_portno; 363 struct usb_device_request req; 364 365 DPRINTF(""); 366 367 /* perform a full reset on the device */ 368 369 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 370 req.bRequest = FTDI_SIO_RESET; 371 USETW(req.wValue, FTDI_SIO_RESET_SIO); 372 USETW(req.wIndex, wIndex); 373 USETW(req.wLength, 0); 374 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 375 &req, NULL, 0, 1000); 376 377 /* turn on RTS/CTS flow control */ 378 379 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 380 req.bRequest = FTDI_SIO_SET_FLOW_CTRL; 381 USETW(req.wValue, 0); 382 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, wIndex); 383 USETW(req.wLength, 0); 384 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 385 &req, NULL, 0, 1000); 386 387 /* 388 * NOTE: with the new UCOM layer there will always be a 389 * "uftdi_cfg_param()" call after "open()", so there is no need for 390 * "open()" to configure anything 391 */ 392 } 393 394 static void 395 uftdi_write_callback(struct usb_xfer *xfer, usb_error_t error) 396 { 397 struct uftdi_softc *sc = usbd_xfer_softc(xfer); 398 struct usb_page_cache *pc; 399 uint32_t actlen; 400 uint8_t buf[1]; 401 402 switch (USB_GET_STATE(xfer)) { 403 case USB_ST_SETUP: 404 case USB_ST_TRANSFERRED: 405 tr_setup: 406 pc = usbd_xfer_get_frame(xfer, 0); 407 if (ucom_get_data(&sc->sc_ucom, pc, 408 sc->sc_hdrlen, UFTDI_OBUFSIZE - sc->sc_hdrlen, 409 &actlen)) { 410 411 if (sc->sc_hdrlen > 0) { 412 buf[0] = 413 FTDI_OUT_TAG(actlen, sc->sc_ucom.sc_portno); 414 usbd_copy_in(pc, 0, buf, 1); 415 } 416 usbd_xfer_set_frame_len(xfer, 0, actlen + sc->sc_hdrlen); 417 usbd_transfer_submit(xfer); 418 } 419 return; 420 421 default: /* Error */ 422 if (error != USB_ERR_CANCELLED) { 423 /* try to clear stall first */ 424 usbd_xfer_set_stall(xfer); 425 goto tr_setup; 426 } 427 return; 428 } 429 } 430 431 static void 432 uftdi_read_callback(struct usb_xfer *xfer, usb_error_t error) 433 { 434 struct uftdi_softc *sc = usbd_xfer_softc(xfer); 435 struct usb_page_cache *pc; 436 uint8_t buf[2]; 437 uint8_t ftdi_msr; 438 uint8_t msr; 439 uint8_t lsr; 440 int actlen; 441 442 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 443 444 switch (USB_GET_STATE(xfer)) { 445 case USB_ST_TRANSFERRED: 446 447 if (actlen < 2) { 448 goto tr_setup; 449 } 450 pc = usbd_xfer_get_frame(xfer, 0); 451 usbd_copy_out(pc, 0, buf, 2); 452 453 ftdi_msr = FTDI_GET_MSR(buf); 454 lsr = FTDI_GET_LSR(buf); 455 456 msr = 0; 457 if (ftdi_msr & FTDI_SIO_CTS_MASK) 458 msr |= SER_CTS; 459 if (ftdi_msr & FTDI_SIO_DSR_MASK) 460 msr |= SER_DSR; 461 if (ftdi_msr & FTDI_SIO_RI_MASK) 462 msr |= SER_RI; 463 if (ftdi_msr & FTDI_SIO_RLSD_MASK) 464 msr |= SER_DCD; 465 466 if ((sc->sc_msr != msr) || 467 ((sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK))) { 468 DPRINTF("status change msr=0x%02x (0x%02x) " 469 "lsr=0x%02x (0x%02x)\n", msr, sc->sc_msr, 470 lsr, sc->sc_lsr); 471 472 sc->sc_msr = msr; 473 sc->sc_lsr = lsr; 474 475 ucom_status_change(&sc->sc_ucom); 476 } 477 actlen -= 2; 478 479 if (actlen > 0) { 480 ucom_put_data(&sc->sc_ucom, pc, 2, actlen); 481 } 482 case USB_ST_SETUP: 483 tr_setup: 484 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 485 usbd_transfer_submit(xfer); 486 return; 487 488 default: /* Error */ 489 if (error != USB_ERR_CANCELLED) { 490 /* try to clear stall first */ 491 usbd_xfer_set_stall(xfer); 492 goto tr_setup; 493 } 494 return; 495 } 496 } 497 498 static void 499 uftdi_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 500 { 501 struct uftdi_softc *sc = ucom->sc_parent; 502 uint16_t wIndex = ucom->sc_portno; 503 uint16_t wValue; 504 struct usb_device_request req; 505 506 wValue = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW; 507 508 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 509 req.bRequest = FTDI_SIO_MODEM_CTRL; 510 USETW(req.wValue, wValue); 511 USETW(req.wIndex, wIndex); 512 USETW(req.wLength, 0); 513 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 514 &req, NULL, 0, 1000); 515 } 516 517 static void 518 uftdi_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 519 { 520 struct uftdi_softc *sc = ucom->sc_parent; 521 uint16_t wIndex = ucom->sc_portno; 522 uint16_t wValue; 523 struct usb_device_request req; 524 525 wValue = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW; 526 527 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 528 req.bRequest = FTDI_SIO_MODEM_CTRL; 529 USETW(req.wValue, wValue); 530 USETW(req.wIndex, wIndex); 531 USETW(req.wLength, 0); 532 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 533 &req, NULL, 0, 1000); 534 } 535 536 static void 537 uftdi_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 538 { 539 struct uftdi_softc *sc = ucom->sc_parent; 540 uint16_t wIndex = ucom->sc_portno; 541 uint16_t wValue; 542 struct usb_device_request req; 543 544 if (onoff) { 545 sc->sc_last_lcr |= FTDI_SIO_SET_BREAK; 546 } else { 547 sc->sc_last_lcr &= ~FTDI_SIO_SET_BREAK; 548 } 549 550 wValue = sc->sc_last_lcr; 551 552 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 553 req.bRequest = FTDI_SIO_SET_DATA; 554 USETW(req.wValue, wValue); 555 USETW(req.wIndex, wIndex); 556 USETW(req.wLength, 0); 557 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 558 &req, NULL, 0, 1000); 559 } 560 561 static int 562 uftdi_set_parm_soft(struct termios *t, 563 struct uftdi_param_config *cfg, uint8_t type) 564 { 565 bzero(cfg, sizeof(*cfg)); 566 567 switch (type) { 568 case UFTDI_TYPE_SIO: 569 switch (t->c_ospeed) { 570 case 300: 571 cfg->rate = ftdi_sio_b300; 572 break; 573 case 600: 574 cfg->rate = ftdi_sio_b600; 575 break; 576 case 1200: 577 cfg->rate = ftdi_sio_b1200; 578 break; 579 case 2400: 580 cfg->rate = ftdi_sio_b2400; 581 break; 582 case 4800: 583 cfg->rate = ftdi_sio_b4800; 584 break; 585 case 9600: 586 cfg->rate = ftdi_sio_b9600; 587 break; 588 case 19200: 589 cfg->rate = ftdi_sio_b19200; 590 break; 591 case 38400: 592 cfg->rate = ftdi_sio_b38400; 593 break; 594 case 57600: 595 cfg->rate = ftdi_sio_b57600; 596 break; 597 case 115200: 598 cfg->rate = ftdi_sio_b115200; 599 break; 600 default: 601 return (EINVAL); 602 } 603 break; 604 605 case UFTDI_TYPE_8U232AM: 606 if (uftdi_8u232am_getrate(t->c_ospeed, &cfg->rate)) { 607 return (EINVAL); 608 } 609 break; 610 } 611 612 if (t->c_cflag & CSTOPB) 613 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_2; 614 else 615 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_1; 616 617 if (t->c_cflag & PARENB) { 618 if (t->c_cflag & PARODD) { 619 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_ODD; 620 } else { 621 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_EVEN; 622 } 623 } else { 624 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_NONE; 625 } 626 627 switch (t->c_cflag & CSIZE) { 628 case CS5: 629 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(5); 630 break; 631 632 case CS6: 633 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(6); 634 break; 635 636 case CS7: 637 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(7); 638 break; 639 640 case CS8: 641 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(8); 642 break; 643 } 644 645 if (t->c_cflag & CRTSCTS) { 646 cfg->v_flow = FTDI_SIO_RTS_CTS_HS; 647 } else if (t->c_iflag & (IXON | IXOFF)) { 648 cfg->v_flow = FTDI_SIO_XON_XOFF_HS; 649 cfg->v_start = t->c_cc[VSTART]; 650 cfg->v_stop = t->c_cc[VSTOP]; 651 } else { 652 cfg->v_flow = FTDI_SIO_DISABLE_FLOW_CTRL; 653 } 654 655 return (0); 656 } 657 658 static int 659 uftdi_pre_param(struct ucom_softc *ucom, struct termios *t) 660 { 661 struct uftdi_softc *sc = ucom->sc_parent; 662 struct uftdi_param_config cfg; 663 664 DPRINTF("\n"); 665 666 return (uftdi_set_parm_soft(t, &cfg, sc->sc_type)); 667 } 668 669 static void 670 uftdi_cfg_param(struct ucom_softc *ucom, struct termios *t) 671 { 672 struct uftdi_softc *sc = ucom->sc_parent; 673 uint16_t wIndex = ucom->sc_portno; 674 struct uftdi_param_config cfg; 675 struct usb_device_request req; 676 677 if (uftdi_set_parm_soft(t, &cfg, sc->sc_type)) { 678 /* should not happen */ 679 return; 680 } 681 sc->sc_last_lcr = cfg.lcr; 682 683 DPRINTF("\n"); 684 685 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 686 req.bRequest = FTDI_SIO_SET_BAUD_RATE; 687 USETW(req.wValue, cfg.rate); 688 USETW(req.wIndex, wIndex); 689 USETW(req.wLength, 0); 690 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 691 &req, NULL, 0, 1000); 692 693 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 694 req.bRequest = FTDI_SIO_SET_DATA; 695 USETW(req.wValue, cfg.lcr); 696 USETW(req.wIndex, wIndex); 697 USETW(req.wLength, 0); 698 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 699 &req, NULL, 0, 1000); 700 701 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 702 req.bRequest = FTDI_SIO_SET_FLOW_CTRL; 703 USETW2(req.wValue, cfg.v_stop, cfg.v_start); 704 USETW2(req.wIndex, cfg.v_flow, wIndex); 705 USETW(req.wLength, 0); 706 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 707 &req, NULL, 0, 1000); 708 } 709 710 static void 711 uftdi_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 712 { 713 struct uftdi_softc *sc = ucom->sc_parent; 714 715 DPRINTF("msr=0x%02x lsr=0x%02x\n", 716 sc->sc_msr, sc->sc_lsr); 717 718 *msr = sc->sc_msr; 719 *lsr = sc->sc_lsr; 720 } 721 722 static void 723 uftdi_start_read(struct ucom_softc *ucom) 724 { 725 struct uftdi_softc *sc = ucom->sc_parent; 726 727 usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_RD]); 728 } 729 730 static void 731 uftdi_stop_read(struct ucom_softc *ucom) 732 { 733 struct uftdi_softc *sc = ucom->sc_parent; 734 735 usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_RD]); 736 } 737 738 static void 739 uftdi_start_write(struct ucom_softc *ucom) 740 { 741 struct uftdi_softc *sc = ucom->sc_parent; 742 743 usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_WR]); 744 } 745 746 static void 747 uftdi_stop_write(struct ucom_softc *ucom) 748 { 749 struct uftdi_softc *sc = ucom->sc_parent; 750 751 usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_WR]); 752 } 753 754 /*------------------------------------------------------------------------* 755 * uftdi_8u232am_getrate 756 * 757 * Return values: 758 * 0: Success 759 * Else: Failure 760 *------------------------------------------------------------------------*/ 761 static uint8_t 762 uftdi_8u232am_getrate(uint32_t speed, uint16_t *rate) 763 { 764 /* Table of the nearest even powers-of-2 for values 0..15. */ 765 static const uint8_t roundoff[16] = { 766 0, 2, 2, 4, 4, 4, 8, 8, 767 8, 8, 8, 8, 16, 16, 16, 16, 768 }; 769 uint32_t d; 770 uint32_t freq; 771 uint16_t result; 772 773 if ((speed < 178) || (speed > ((3000000 * 100) / 97))) 774 return (1); /* prevent numerical overflow */ 775 776 /* Special cases for 2M and 3M. */ 777 if ((speed >= ((3000000 * 100) / 103)) && 778 (speed <= ((3000000 * 100) / 97))) { 779 result = 0; 780 goto done; 781 } 782 if ((speed >= ((2000000 * 100) / 103)) && 783 (speed <= ((2000000 * 100) / 97))) { 784 result = 1; 785 goto done; 786 } 787 d = (FTDI_8U232AM_FREQ << 4) / speed; 788 d = (d & ~15) + roundoff[d & 15]; 789 790 if (d < FTDI_8U232AM_MIN_DIV) 791 d = FTDI_8U232AM_MIN_DIV; 792 else if (d > FTDI_8U232AM_MAX_DIV) 793 d = FTDI_8U232AM_MAX_DIV; 794 795 /* 796 * Calculate the frequency needed for "d" to exactly divide down to 797 * our target "speed", and check that the actual frequency is within 798 * 3% of this. 799 */ 800 freq = (speed * d); 801 if ((freq < ((FTDI_8U232AM_FREQ * 1600ULL) / 103)) || 802 (freq > ((FTDI_8U232AM_FREQ * 1600ULL) / 97))) 803 return (1); 804 805 /* 806 * Pack the divisor into the resultant value. The lower 14-bits 807 * hold the integral part, while the upper 2 bits encode the 808 * fractional component: either 0, 0.5, 0.25, or 0.125. 809 */ 810 result = (d >> 4); 811 if (d & 8) 812 result |= 0x4000; 813 else if (d & 4) 814 result |= 0x8000; 815 else if (d & 2) 816 result |= 0xc000; 817 818 done: 819 *rate = result; 820 return (0); 821 } 822 823 static void 824 uftdi_poll(struct ucom_softc *ucom) 825 { 826 struct uftdi_softc *sc = ucom->sc_parent; 827 usbd_transfer_poll(sc->sc_xfer, UFTDI_N_TRANSFER); 828 } 829