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