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