| 1 | /* $NetBSD: uftdi.c,v 1.62 2016/07/07 06:55:42 msaitoh 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.62 2016/07/07 06:55:42 msaitoh Exp $" ); |
| 34 | |
| 35 | #include <sys/param.h> |
| 36 | #include <sys/systm.h> |
| 37 | #include <sys/kernel.h> |
| 38 | #include <sys/device.h> |
| 39 | #include <sys/conf.h> |
| 40 | #include <sys/tty.h> |
| 41 | |
| 42 | #include <dev/usb/usb.h> |
| 43 | |
| 44 | #include <dev/usb/usbdi.h> |
| 45 | #include <dev/usb/usbdi_util.h> |
| 46 | #include <dev/usb/usbdevs.h> |
| 47 | |
| 48 | #include <dev/usb/ucomvar.h> |
| 49 | |
| 50 | #include <dev/usb/uftdireg.h> |
| 51 | |
| 52 | #ifdef UFTDI_DEBUG |
| 53 | #define DPRINTF(x) if (uftdidebug) printf x |
| 54 | #define DPRINTFN(n,x) if (uftdidebug>(n)) printf x |
| 55 | int uftdidebug = 0; |
| 56 | #else |
| 57 | #define DPRINTF(x) |
| 58 | #define DPRINTFN(n,x) |
| 59 | #endif |
| 60 | |
| 61 | #define UFTDI_CONFIG_INDEX 0 |
| 62 | #define UFTDI_IFACE_INDEX 0 |
| 63 | #define UFTDI_MAX_PORTS 4 |
| 64 | |
| 65 | /* |
| 66 | * These are the default number of bytes transferred per frame if the |
| 67 | * endpoint doesn't tell us. The output buffer size is a hard limit |
| 68 | * for devices that use a 6-bit size encoding. |
| 69 | */ |
| 70 | #define UFTDIIBUFSIZE 64 |
| 71 | #define UFTDIOBUFSIZE 64 |
| 72 | |
| 73 | /* |
| 74 | * Magic constants! Where do these come from? They're what Linux uses... |
| 75 | */ |
| 76 | #define UFTDI_MAX_IBUFSIZE 512 |
| 77 | #define UFTDI_MAX_OBUFSIZE 256 |
| 78 | |
| 79 | struct uftdi_softc { |
| 80 | device_t sc_dev; /* base device */ |
| 81 | struct usbd_device * sc_udev; /* device */ |
| 82 | struct usbd_interface * sc_iface[UFTDI_MAX_PORTS]; /* interface */ |
| 83 | |
| 84 | enum uftdi_type sc_type; |
| 85 | u_int sc_hdrlen; |
| 86 | u_int sc_numports; |
| 87 | u_int sc_chiptype; |
| 88 | |
| 89 | u_char sc_msr; |
| 90 | u_char sc_lsr; |
| 91 | |
| 92 | device_t sc_subdev[UFTDI_MAX_PORTS]; |
| 93 | |
| 94 | u_char sc_dying; |
| 95 | |
| 96 | u_int last_lcr; |
| 97 | |
| 98 | }; |
| 99 | |
| 100 | Static void uftdi_get_status(void *, int, u_char *, u_char *); |
| 101 | Static void uftdi_set(void *, int, int, int); |
| 102 | Static int uftdi_param(void *, int, struct termios *); |
| 103 | Static int uftdi_open(void *, int); |
| 104 | Static void uftdi_read(void *, int, u_char **, uint32_t *); |
| 105 | Static void uftdi_write(void *, int, u_char *, u_char *, |
| 106 | uint32_t *); |
| 107 | Static void uftdi_break(void *, int, int); |
| 108 | |
| 109 | struct ucom_methods uftdi_methods = { |
| 110 | .ucom_get_status = uftdi_get_status, |
| 111 | .ucom_set = uftdi_set, |
| 112 | .ucom_param = uftdi_param, |
| 113 | .ucom_ioctl = NULL, |
| 114 | .ucom_open = uftdi_open, |
| 115 | .ucom_close = NULL, |
| 116 | .ucom_read = uftdi_read, |
| 117 | .ucom_write = uftdi_write, |
| 118 | }; |
| 119 | |
| 120 | /* |
| 121 | * The devices default to UFTDI_TYPE_8U232AM. |
| 122 | * Remember to update uftdi_attach() if it should be UFTDI_TYPE_SIO instead |
| 123 | */ |
| 124 | static const struct usb_devno uftdi_devs[] = { |
| 125 | { USB_VENDOR_BBELECTRONICS, USB_PRODUCT_BBELECTRONICS_USOTL4 }, |
| 126 | { USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_TWIST }, |
| 127 | { USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_SAMBA }, |
| 128 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_230X }, |
| 129 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232H }, |
| 130 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232RL }, |
| 131 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_2232C }, |
| 132 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_4232H }, |
| 133 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U100AX }, |
| 134 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM }, |
| 135 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_KW }, |
| 136 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_YS }, |
| 137 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y6 }, |
| 138 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y8 }, |
| 139 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_IC }, |
| 140 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_DB9 }, |
| 141 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_RS232 }, |
| 142 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y9 }, |
| 143 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_COASTAL_TNCX }, |
| 144 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_485_MINI }, |
| 145 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_NANO_485 }, |
| 146 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20 }, |
| 147 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK202_24_USB }, |
| 148 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK204_24_USB }, |
| 149 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX200_USB }, |
| 150 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX4_MX5_USB }, |
| 151 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_631 }, |
| 152 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_632 }, |
| 153 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_633 }, |
| 154 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_634 }, |
| 155 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_635 }, |
| 156 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_OPENRD_JTAGKEY }, |
| 157 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_BEAGLEBONE }, |
| 158 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MAXSTREAM_PKG_U }, |
| 159 | { USB_VENDOR_xxFTDI, USB_PRODUCT_xxFTDI_SHEEVAPLUG_JTAG }, |
| 160 | { USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_VALUECAN }, |
| 161 | { USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_NEOVI }, |
| 162 | { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_PCOPRS1 }, |
| 163 | { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60F }, |
| 164 | { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_USBSERIAL }, |
| 165 | { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P1 }, |
| 166 | { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P2 }, |
| 167 | { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P3 }, |
| 168 | { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P4 }, |
| 169 | { USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_US2308 }, |
| 170 | { USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK }, |
| 171 | { USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK_DUO }, |
| 172 | }; |
| 173 | #define uftdi_lookup(v, p) usb_lookup(uftdi_devs, v, p) |
| 174 | |
| 175 | int uftdi_match(device_t, cfdata_t, void *); |
| 176 | void uftdi_attach(device_t, device_t, void *); |
| 177 | void uftdi_childdet(device_t, device_t); |
| 178 | int uftdi_detach(device_t, int); |
| 179 | int uftdi_activate(device_t, enum devact); |
| 180 | extern struct cfdriver uftdi_cd; |
| 181 | CFATTACH_DECL2_NEW(uftdi, sizeof(struct uftdi_softc), uftdi_match, |
| 182 | uftdi_attach, uftdi_detach, uftdi_activate, NULL, uftdi_childdet); |
| 183 | |
| 184 | int |
| 185 | uftdi_match(device_t parent, cfdata_t match, void *aux) |
| 186 | { |
| 187 | struct usb_attach_arg *uaa = aux; |
| 188 | |
| 189 | DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n" , |
| 190 | uaa->uaa_vendor, uaa->uaa_product)); |
| 191 | |
| 192 | return uftdi_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ? |
| 193 | UMATCH_VENDOR_PRODUCT : UMATCH_NONE; |
| 194 | } |
| 195 | |
| 196 | void |
| 197 | uftdi_attach(device_t parent, device_t self, void *aux) |
| 198 | { |
| 199 | struct uftdi_softc *sc = device_private(self); |
| 200 | struct usb_attach_arg *uaa = aux; |
| 201 | struct usbd_device *dev = uaa->uaa_device; |
| 202 | struct usbd_interface *iface; |
| 203 | usb_device_descriptor_t *ddesc; |
| 204 | usb_interface_descriptor_t *id; |
| 205 | usb_endpoint_descriptor_t *ed; |
| 206 | char *devinfop; |
| 207 | const char *devname = device_xname(self); |
| 208 | int i,idx; |
| 209 | usbd_status err; |
| 210 | struct ucom_attach_args ucaa; |
| 211 | |
| 212 | DPRINTFN(10,("\nuftdi_attach: sc=%p\n" , sc)); |
| 213 | |
| 214 | aprint_naive("\n" ); |
| 215 | aprint_normal("\n" ); |
| 216 | |
| 217 | devinfop = usbd_devinfo_alloc(dev, 0); |
| 218 | aprint_normal_dev(self, "%s\n" , devinfop); |
| 219 | usbd_devinfo_free(devinfop); |
| 220 | |
| 221 | /* Move the device into the configured state. */ |
| 222 | err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1); |
| 223 | if (err) { |
| 224 | aprint_error("\n%s: failed to set configuration, err=%s\n" , |
| 225 | devname, usbd_errstr(err)); |
| 226 | goto bad; |
| 227 | } |
| 228 | |
| 229 | sc->sc_dev = self; |
| 230 | sc->sc_udev = dev; |
| 231 | sc->sc_numports = 1; |
| 232 | sc->sc_type = UFTDI_TYPE_8U232AM; /* most devices are post-8U232AM */ |
| 233 | sc->sc_hdrlen = 0; |
| 234 | if (uaa->uaa_vendor == USB_VENDOR_FTDI |
| 235 | && uaa->uaa_product == USB_PRODUCT_FTDI_SERIAL_8U100AX) { |
| 236 | sc->sc_type = UFTDI_TYPE_SIO; |
| 237 | sc->sc_hdrlen = 1; |
| 238 | } |
| 239 | |
| 240 | ddesc = usbd_get_device_descriptor(dev); |
| 241 | sc->sc_chiptype = UGETW(ddesc->bcdDevice); |
| 242 | switch (sc->sc_chiptype) { |
| 243 | case 0x500: /* 2232D */ |
| 244 | case 0x700: /* 2232H */ |
| 245 | sc->sc_numports = 2; |
| 246 | break; |
| 247 | case 0x800: /* 4232H */ |
| 248 | sc->sc_numports = 4; |
| 249 | break; |
| 250 | case 0x200: /* 232/245AM */ |
| 251 | case 0x400: /* 232/245BL */ |
| 252 | case 0x600: /* 232/245R */ |
| 253 | default: |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | for (idx = UFTDI_IFACE_INDEX; idx < sc->sc_numports; idx++) { |
| 258 | err = usbd_device2interface_handle(dev, idx, &iface); |
| 259 | if (err) { |
| 260 | aprint_error( |
| 261 | "\n%s: failed to get interface idx=%d, err=%s\n" , |
| 262 | devname, idx, usbd_errstr(err)); |
| 263 | goto bad; |
| 264 | } |
| 265 | |
| 266 | id = usbd_get_interface_descriptor(iface); |
| 267 | |
| 268 | sc->sc_iface[idx] = iface; |
| 269 | |
| 270 | ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1; |
| 271 | ucaa.ucaa_ibufsize = ucaa.ucaa_obufsize = 0; |
| 272 | for (i = 0; i < id->bNumEndpoints; i++) { |
| 273 | int addr, dir, attr; |
| 274 | ed = usbd_interface2endpoint_descriptor(iface, i); |
| 275 | if (ed == NULL) { |
| 276 | aprint_error_dev(self, |
| 277 | "could not read endpoint descriptor: %s\n" , |
| 278 | usbd_errstr(err)); |
| 279 | goto bad; |
| 280 | } |
| 281 | |
| 282 | addr = ed->bEndpointAddress; |
| 283 | dir = UE_GET_DIR(ed->bEndpointAddress); |
| 284 | attr = ed->bmAttributes & UE_XFERTYPE; |
| 285 | if (dir == UE_DIR_IN && attr == UE_BULK) { |
| 286 | ucaa.ucaa_bulkin = addr; |
| 287 | ucaa.ucaa_ibufsize = UGETW(ed->wMaxPacketSize); |
| 288 | if (ucaa.ucaa_ibufsize >= UFTDI_MAX_IBUFSIZE) |
| 289 | ucaa.ucaa_ibufsize = UFTDI_MAX_IBUFSIZE; |
| 290 | } else if (dir == UE_DIR_OUT && attr == UE_BULK) { |
| 291 | ucaa.ucaa_bulkout = addr; |
| 292 | ucaa.ucaa_obufsize = UGETW(ed->wMaxPacketSize) |
| 293 | - sc->sc_hdrlen; |
| 294 | if (ucaa.ucaa_obufsize >= UFTDI_MAX_OBUFSIZE) |
| 295 | ucaa.ucaa_obufsize = UFTDI_MAX_OBUFSIZE; |
| 296 | /* Limit length if we have a 6-bit header. */ |
| 297 | if ((sc->sc_hdrlen > 0) && |
| 298 | (ucaa.ucaa_obufsize > UFTDIOBUFSIZE)) |
| 299 | ucaa.ucaa_obufsize = UFTDIOBUFSIZE; |
| 300 | } else { |
| 301 | aprint_error_dev(self, |
| 302 | "unexpected endpoint\n" ); |
| 303 | goto bad; |
| 304 | } |
| 305 | } |
| 306 | if (ucaa.ucaa_bulkin == -1) { |
| 307 | aprint_error_dev(self, |
| 308 | "Could not find data bulk in\n" ); |
| 309 | goto bad; |
| 310 | } |
| 311 | if (ucaa.ucaa_bulkout == -1) { |
| 312 | aprint_error_dev(self, |
| 313 | "Could not find data bulk out\n" ); |
| 314 | goto bad; |
| 315 | } |
| 316 | |
| 317 | ucaa.ucaa_portno = FTDI_PIT_SIOA + idx; |
| 318 | /* ucaa_bulkin, ucaa_bulkout set above */ |
| 319 | if (ucaa.ucaa_ibufsize == 0) |
| 320 | ucaa.ucaa_ibufsize = UFTDIIBUFSIZE; |
| 321 | ucaa.ucaa_ibufsizepad = ucaa.ucaa_ibufsize; |
| 322 | if (ucaa.ucaa_obufsize == 0) |
| 323 | ucaa.ucaa_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen; |
| 324 | ucaa.ucaa_opkthdrlen = sc->sc_hdrlen; |
| 325 | ucaa.ucaa_device = dev; |
| 326 | ucaa.ucaa_iface = iface; |
| 327 | ucaa.ucaa_methods = &uftdi_methods; |
| 328 | ucaa.ucaa_arg = sc; |
| 329 | ucaa.ucaa_info = NULL; |
| 330 | |
| 331 | DPRINTF(("uftdi: in=0x%x out=0x%x isize=0x%x osize=0x%x\n" , |
| 332 | ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, |
| 333 | ucaa.ucaa_ibufsize, ucaa.ucaa_obufsize)); |
| 334 | sc->sc_subdev[idx] = config_found_sm_loc(self, "ucombus" , NULL, |
| 335 | &ucaa, ucomprint, ucomsubmatch); |
| 336 | } |
| 337 | |
| 338 | usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); |
| 339 | |
| 340 | return; |
| 341 | |
| 342 | bad: |
| 343 | DPRINTF(("uftdi_attach: ATTACH ERROR\n" )); |
| 344 | sc->sc_dying = 1; |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | int |
| 349 | uftdi_activate(device_t self, enum devact act) |
| 350 | { |
| 351 | struct uftdi_softc *sc = device_private(self); |
| 352 | |
| 353 | switch (act) { |
| 354 | case DVACT_DEACTIVATE: |
| 355 | sc->sc_dying = 1; |
| 356 | return 0; |
| 357 | default: |
| 358 | return EOPNOTSUPP; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void |
| 363 | uftdi_childdet(device_t self, device_t child) |
| 364 | { |
| 365 | int i; |
| 366 | struct uftdi_softc *sc = device_private(self); |
| 367 | |
| 368 | for (i = 0; i < sc->sc_numports; i++) { |
| 369 | if (sc->sc_subdev[i] == child) |
| 370 | break; |
| 371 | } |
| 372 | KASSERT(i < sc->sc_numports); |
| 373 | sc->sc_subdev[i] = NULL; |
| 374 | } |
| 375 | |
| 376 | int |
| 377 | uftdi_detach(device_t self, int flags) |
| 378 | { |
| 379 | struct uftdi_softc *sc = device_private(self); |
| 380 | int i; |
| 381 | |
| 382 | DPRINTF(("uftdi_detach: sc=%p flags=%d\n" , sc, flags)); |
| 383 | sc->sc_dying = 1; |
| 384 | for (i=0; i < sc->sc_numports; i++) { |
| 385 | if (sc->sc_subdev[i] != NULL) |
| 386 | config_detach(sc->sc_subdev[i], flags); |
| 387 | } |
| 388 | |
| 389 | usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | Static int |
| 395 | uftdi_open(void *vsc, int portno) |
| 396 | { |
| 397 | struct uftdi_softc *sc = vsc; |
| 398 | usb_device_request_t req; |
| 399 | usbd_status err; |
| 400 | struct termios t; |
| 401 | |
| 402 | DPRINTF(("uftdi_open: sc=%p\n" , sc)); |
| 403 | |
| 404 | if (sc->sc_dying) |
| 405 | return EIO; |
| 406 | |
| 407 | /* Perform a full reset on the device */ |
| 408 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 409 | req.bRequest = FTDI_SIO_RESET; |
| 410 | USETW(req.wValue, FTDI_SIO_RESET_SIO); |
| 411 | USETW(req.wIndex, portno); |
| 412 | USETW(req.wLength, 0); |
| 413 | err = usbd_do_request(sc->sc_udev, &req, NULL); |
| 414 | if (err) |
| 415 | return EIO; |
| 416 | |
| 417 | /* Set 9600 baud, 2 stop bits, no parity, 8 bits */ |
| 418 | t.c_ospeed = 9600; |
| 419 | t.c_cflag = CSTOPB | CS8; |
| 420 | (void)uftdi_param(sc, portno, &t); |
| 421 | |
| 422 | /* Turn on RTS/CTS flow control */ |
| 423 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 424 | req.bRequest = FTDI_SIO_SET_FLOW_CTRL; |
| 425 | USETW(req.wValue, 0); |
| 426 | USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno); |
| 427 | USETW(req.wLength, 0); |
| 428 | err = usbd_do_request(sc->sc_udev, &req, NULL); |
| 429 | if (err) |
| 430 | return EIO; |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | Static void |
| 436 | uftdi_read(void *vsc, int portno, u_char **ptr, uint32_t *count) |
| 437 | { |
| 438 | struct uftdi_softc *sc = vsc; |
| 439 | u_char msr, lsr; |
| 440 | |
| 441 | DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n" , sc, portno, |
| 442 | *count)); |
| 443 | |
| 444 | msr = FTDI_GET_MSR(*ptr); |
| 445 | lsr = FTDI_GET_LSR(*ptr); |
| 446 | |
| 447 | #ifdef UFTDI_DEBUG |
| 448 | if (*count != 2) |
| 449 | DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]=" |
| 450 | "0x%02x\n" , sc, portno, *count, (*ptr)[2])); |
| 451 | #endif |
| 452 | |
| 453 | if (sc->sc_msr != msr || |
| 454 | (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) { |
| 455 | DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) " |
| 456 | "lsr=0x%02x(0x%02x)\n" , msr, sc->sc_msr, |
| 457 | lsr, sc->sc_lsr)); |
| 458 | sc->sc_msr = msr; |
| 459 | sc->sc_lsr = lsr; |
| 460 | ucom_status_change(device_private(sc->sc_subdev[portno-1])); |
| 461 | } |
| 462 | |
| 463 | /* Adjust buffer pointer to skip status prefix */ |
| 464 | *ptr += 2; |
| 465 | } |
| 466 | |
| 467 | Static void |
| 468 | uftdi_write(void *vsc, int portno, u_char *to, u_char *from, uint32_t *count) |
| 469 | { |
| 470 | struct uftdi_softc *sc = vsc; |
| 471 | |
| 472 | DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n" , |
| 473 | vsc, portno, *count, from[0])); |
| 474 | |
| 475 | /* Make length tag and copy data */ |
| 476 | if (sc->sc_hdrlen > 0) |
| 477 | *to = FTDI_OUT_TAG(*count, portno); |
| 478 | |
| 479 | memcpy(to + sc->sc_hdrlen, from, *count); |
| 480 | *count += sc->sc_hdrlen; |
| 481 | } |
| 482 | |
| 483 | Static void |
| 484 | uftdi_set(void *vsc, int portno, int reg, int onoff) |
| 485 | { |
| 486 | struct uftdi_softc *sc = vsc; |
| 487 | usb_device_request_t req; |
| 488 | int ctl; |
| 489 | |
| 490 | DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n" , vsc, portno, |
| 491 | reg, onoff)); |
| 492 | |
| 493 | switch (reg) { |
| 494 | case UCOM_SET_DTR: |
| 495 | ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW; |
| 496 | break; |
| 497 | case UCOM_SET_RTS: |
| 498 | ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW; |
| 499 | break; |
| 500 | case UCOM_SET_BREAK: |
| 501 | uftdi_break(sc, portno, onoff); |
| 502 | return; |
| 503 | default: |
| 504 | return; |
| 505 | } |
| 506 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 507 | req.bRequest = FTDI_SIO_MODEM_CTRL; |
| 508 | USETW(req.wValue, ctl); |
| 509 | USETW(req.wIndex, portno); |
| 510 | USETW(req.wLength, 0); |
| 511 | DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x " |
| 512 | "index=0x%04x len=%d\n" , req.bmRequestType, req.bRequest, |
| 513 | UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); |
| 514 | (void)usbd_do_request(sc->sc_udev, &req, NULL); |
| 515 | } |
| 516 | |
| 517 | Static int |
| 518 | uftdi_param(void *vsc, int portno, struct termios *t) |
| 519 | { |
| 520 | struct uftdi_softc *sc = vsc; |
| 521 | usb_device_request_t req; |
| 522 | usbd_status err; |
| 523 | int rate, data, flow; |
| 524 | |
| 525 | DPRINTF(("uftdi_param: sc=%p\n" , sc)); |
| 526 | |
| 527 | if (sc->sc_dying) |
| 528 | return EIO; |
| 529 | |
| 530 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 531 | req.bRequest = FTDI_SIO_SET_BITMODE; |
| 532 | USETW(req.wValue, FTDI_BITMODE_RESET << 8 | 0x00); |
| 533 | USETW(req.wIndex, portno); |
| 534 | USETW(req.wLength, 0); |
| 535 | err = usbd_do_request(sc->sc_udev, &req, NULL); |
| 536 | if (err) |
| 537 | return EIO; |
| 538 | |
| 539 | switch (sc->sc_type) { |
| 540 | case UFTDI_TYPE_SIO: |
| 541 | switch (t->c_ospeed) { |
| 542 | case 300: rate = ftdi_sio_b300; break; |
| 543 | case 600: rate = ftdi_sio_b600; break; |
| 544 | case 1200: rate = ftdi_sio_b1200; break; |
| 545 | case 2400: rate = ftdi_sio_b2400; break; |
| 546 | case 4800: rate = ftdi_sio_b4800; break; |
| 547 | case 9600: rate = ftdi_sio_b9600; break; |
| 548 | case 19200: rate = ftdi_sio_b19200; break; |
| 549 | case 38400: rate = ftdi_sio_b38400; break; |
| 550 | case 57600: rate = ftdi_sio_b57600; break; |
| 551 | case 115200: rate = ftdi_sio_b115200; break; |
| 552 | default: |
| 553 | return EINVAL; |
| 554 | } |
| 555 | break; |
| 556 | |
| 557 | case UFTDI_TYPE_8U232AM: |
| 558 | switch(t->c_ospeed) { |
| 559 | case 300: rate = ftdi_8u232am_b300; break; |
| 560 | case 600: rate = ftdi_8u232am_b600; break; |
| 561 | case 1200: rate = ftdi_8u232am_b1200; break; |
| 562 | case 2400: rate = ftdi_8u232am_b2400; break; |
| 563 | case 4800: rate = ftdi_8u232am_b4800; break; |
| 564 | case 9600: rate = ftdi_8u232am_b9600; break; |
| 565 | case 19200: rate = ftdi_8u232am_b19200; break; |
| 566 | case 38400: rate = ftdi_8u232am_b38400; break; |
| 567 | case 57600: rate = ftdi_8u232am_b57600; break; |
| 568 | case 115200: rate = ftdi_8u232am_b115200; break; |
| 569 | case 230400: rate = ftdi_8u232am_b230400; break; |
| 570 | case 460800: rate = ftdi_8u232am_b460800; break; |
| 571 | case 921600: rate = ftdi_8u232am_b921600; break; |
| 572 | default: |
| 573 | return EINVAL; |
| 574 | } |
| 575 | break; |
| 576 | |
| 577 | default: |
| 578 | return EINVAL; |
| 579 | } |
| 580 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 581 | req.bRequest = FTDI_SIO_SET_BAUD_RATE; |
| 582 | USETW(req.wValue, rate); |
| 583 | USETW(req.wIndex, portno); |
| 584 | USETW(req.wLength, 0); |
| 585 | DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x " |
| 586 | "index=0x%04x len=%d\n" , req.bmRequestType, req.bRequest, |
| 587 | UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); |
| 588 | err = usbd_do_request(sc->sc_udev, &req, NULL); |
| 589 | if (err) |
| 590 | return EIO; |
| 591 | |
| 592 | if (ISSET(t->c_cflag, CSTOPB)) |
| 593 | data = FTDI_SIO_SET_DATA_STOP_BITS_2; |
| 594 | else |
| 595 | data = FTDI_SIO_SET_DATA_STOP_BITS_1; |
| 596 | if (ISSET(t->c_cflag, PARENB)) { |
| 597 | if (ISSET(t->c_cflag, PARODD)) |
| 598 | data |= FTDI_SIO_SET_DATA_PARITY_ODD; |
| 599 | else |
| 600 | data |= FTDI_SIO_SET_DATA_PARITY_EVEN; |
| 601 | } else |
| 602 | data |= FTDI_SIO_SET_DATA_PARITY_NONE; |
| 603 | switch (ISSET(t->c_cflag, CSIZE)) { |
| 604 | case CS5: |
| 605 | data |= FTDI_SIO_SET_DATA_BITS(5); |
| 606 | break; |
| 607 | case CS6: |
| 608 | data |= FTDI_SIO_SET_DATA_BITS(6); |
| 609 | break; |
| 610 | case CS7: |
| 611 | data |= FTDI_SIO_SET_DATA_BITS(7); |
| 612 | break; |
| 613 | case CS8: |
| 614 | data |= FTDI_SIO_SET_DATA_BITS(8); |
| 615 | break; |
| 616 | } |
| 617 | sc->last_lcr = data; |
| 618 | |
| 619 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 620 | req.bRequest = FTDI_SIO_SET_DATA; |
| 621 | USETW(req.wValue, data); |
| 622 | USETW(req.wIndex, portno); |
| 623 | USETW(req.wLength, 0); |
| 624 | DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x " |
| 625 | "index=0x%04x len=%d\n" , req.bmRequestType, req.bRequest, |
| 626 | UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); |
| 627 | err = usbd_do_request(sc->sc_udev, &req, NULL); |
| 628 | if (err) |
| 629 | return EIO; |
| 630 | |
| 631 | if (ISSET(t->c_cflag, CRTSCTS)) { |
| 632 | flow = FTDI_SIO_RTS_CTS_HS; |
| 633 | USETW(req.wValue, 0); |
| 634 | } else if (ISSET(t->c_iflag, IXON) && ISSET(t->c_iflag, IXOFF)) { |
| 635 | flow = FTDI_SIO_XON_XOFF_HS; |
| 636 | USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]); |
| 637 | } else { |
| 638 | flow = FTDI_SIO_DISABLE_FLOW_CTRL; |
| 639 | USETW(req.wValue, 0); |
| 640 | } |
| 641 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 642 | req.bRequest = FTDI_SIO_SET_FLOW_CTRL; |
| 643 | USETW2(req.wIndex, flow, portno); |
| 644 | USETW(req.wLength, 0); |
| 645 | err = usbd_do_request(sc->sc_udev, &req, NULL); |
| 646 | if (err) |
| 647 | return EIO; |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | void |
| 653 | uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr) |
| 654 | { |
| 655 | struct uftdi_softc *sc = vsc; |
| 656 | |
| 657 | DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n" , |
| 658 | sc->sc_msr, sc->sc_lsr)); |
| 659 | |
| 660 | if (msr != NULL) |
| 661 | *msr = sc->sc_msr; |
| 662 | if (lsr != NULL) |
| 663 | *lsr = sc->sc_lsr; |
| 664 | } |
| 665 | |
| 666 | void |
| 667 | uftdi_break(void *vsc, int portno, int onoff) |
| 668 | { |
| 669 | struct uftdi_softc *sc = vsc; |
| 670 | usb_device_request_t req; |
| 671 | int data; |
| 672 | |
| 673 | DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n" , vsc, portno, |
| 674 | onoff)); |
| 675 | |
| 676 | if (onoff) { |
| 677 | data = sc->last_lcr | FTDI_SIO_SET_BREAK; |
| 678 | } else { |
| 679 | data = sc->last_lcr; |
| 680 | } |
| 681 | |
| 682 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 683 | req.bRequest = FTDI_SIO_SET_DATA; |
| 684 | USETW(req.wValue, data); |
| 685 | USETW(req.wIndex, portno); |
| 686 | USETW(req.wLength, 0); |
| 687 | (void)usbd_do_request(sc->sc_udev, &req, NULL); |
| 688 | } |
| 689 | |