| 1 | /* |
| 2 | * $NetBSD: uftdi.c,v 1.13 2002/09/23 05:51:23 simonb Exp $ |
| 3 | * $FreeBSD: src/sys/dev/usb/uftdi.c,v 1.37 2007/06/22 05:53:05 imp Exp $ |
| 4 | * $DragonFly: src/sys/dev/usbmisc/uftdi/uftdi.c,v 1.16 2007/08/31 13:25:32 hasso Exp $ |
| 5 | */ |
| 6 | |
| 7 | /*- |
| 8 | * Copyright (c) 2000 The NetBSD Foundation, Inc. |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This code is derived from software contributed to The NetBSD Foundation |
| 12 | * by Lennart Augustsson (lennart@augustsson.net). |
| 13 | * |
| 14 | * Redistribution and use in source and binary forms, with or without |
| 15 | * modification, are permitted provided that the following conditions |
| 16 | * are met: |
| 17 | * 1. Redistributions of source code must retain the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer. |
| 19 | * 2. Redistributions in binary form must reproduce the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer in the |
| 21 | * documentation and/or other materials provided with the distribution. |
| 22 | * 3. All advertising materials mentioning features or use of this software |
| 23 | * must display the following acknowledgement: |
| 24 | * This product includes software developed by the NetBSD |
| 25 | * Foundation, Inc. and its contributors. |
| 26 | * 4. Neither the name of The NetBSD Foundation nor the names of its |
| 27 | * contributors may be used to endorse or promote products derived |
| 28 | * from this software without specific prior written permission. |
| 29 | * |
| 30 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 31 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 32 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 33 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 34 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 36 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 37 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 38 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 39 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 40 | * POSSIBILITY OF SUCH DAMAGE. |
| 41 | */ |
| 42 | |
| 43 | /* |
| 44 | * FTDI FT8U100AX serial adapter driver |
| 45 | */ |
| 46 | |
| 47 | #include <sys/cdefs.h> |
| 48 | |
| 49 | #include <sys/param.h> |
| 50 | #include <sys/systm.h> |
| 51 | #include <sys/kernel.h> |
| 52 | #include <sys/malloc.h> |
| 53 | #include <sys/module.h> |
| 54 | #include <sys/bus.h> |
| 55 | #include <sys/ioccom.h> |
| 56 | #include <sys/fcntl.h> |
| 57 | #include <sys/conf.h> |
| 58 | #include <sys/tty.h> |
| 59 | #include <sys/file.h> |
| 60 | |
| 61 | #include <sys/select.h> |
| 62 | |
| 63 | #include <sys/sysctl.h> |
| 64 | |
| 65 | #include <bus/usb/usb.h> |
| 66 | #include <bus/usb/usbhid.h> |
| 67 | |
| 68 | #include <bus/usb/usbdi.h> |
| 69 | #include <bus/usb/usbdi_util.h> |
| 70 | #include <bus/usb/usbdevs.h> |
| 71 | |
| 72 | #include "../ucom/ucomvar.h" |
| 73 | |
| 74 | #include "uftdireg.h" |
| 75 | |
| 76 | #ifdef USB_DEBUG |
| 77 | static int uftdidebug = 0; |
| 78 | SYSCTL_NODE(_hw_usb, OID_AUTO, uftdi, CTLFLAG_RW, 0, "USB uftdi"); |
| 79 | SYSCTL_INT(_hw_usb_uftdi, OID_AUTO, debug, CTLFLAG_RW, |
| 80 | &uftdidebug, 0, "uftdi debug level"); |
| 81 | #define DPRINTF(x) do { \ |
| 82 | if (uftdidebug) \ |
| 83 | kprintf x; \ |
| 84 | } while (0) |
| 85 | |
| 86 | #define DPRINTFN(n, x) do { \ |
| 87 | if (uftdidebug > (n)) \ |
| 88 | kprintf x; \ |
| 89 | } while (0) |
| 90 | |
| 91 | #else |
| 92 | #define DPRINTF(x) |
| 93 | #define DPRINTFN(n,x) |
| 94 | #endif |
| 95 | |
| 96 | #define UFTDI_CONFIG_INDEX 0 |
| 97 | #define UFTDI_IFACE_INDEX 0 |
| 98 | |
| 99 | /* |
| 100 | * These are the maximum number of bytes transferred per frame. |
| 101 | * The output buffer size cannot be increased due to the size encoding. |
| 102 | */ |
| 103 | #define UFTDIIBUFSIZE 64 |
| 104 | #define UFTDIOBUFSIZE 64 |
| 105 | |
| 106 | struct uftdi_softc { |
| 107 | struct ucom_softc sc_ucom; |
| 108 | usbd_interface_handle sc_iface; /* interface */ |
| 109 | enum uftdi_type sc_type; |
| 110 | u_int sc_hdrlen; |
| 111 | u_char sc_msr; |
| 112 | u_char sc_lsr; |
| 113 | u_int last_lcr; |
| 114 | }; |
| 115 | |
| 116 | static void uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr); |
| 117 | static void uftdi_set(void *, int, int, int); |
| 118 | static int uftdi_param(void *, int, struct termios *); |
| 119 | static int uftdi_open(void *sc, int portno); |
| 120 | static void uftdi_read(void *sc, int portno, u_char **ptr,u_int32_t *count); |
| 121 | static void uftdi_write(void *sc, int portno, u_char *to, u_char *from, |
| 122 | u_int32_t *count); |
| 123 | static void uftdi_break(void *sc, int portno, int onoff); |
| 124 | |
| 125 | struct ucom_callback uftdi_callback = { |
| 126 | uftdi_get_status, |
| 127 | uftdi_set, |
| 128 | uftdi_param, |
| 129 | NULL, |
| 130 | uftdi_open, |
| 131 | NULL, |
| 132 | uftdi_read, |
| 133 | uftdi_write, |
| 134 | }; |
| 135 | |
| 136 | static const struct usb_devno uftdi_devs[] = { |
| 137 | { USB_VENDOR_BBELECTRONICS, USB_PRODUCT_BBELECTRONICS_USOTL4 }, |
| 138 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U100AX }, |
| 139 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM }, |
| 140 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_2232C }, |
| 141 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20 }, |
| 142 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_631 }, |
| 143 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_632 }, |
| 144 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_633 }, |
| 145 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_634 }, |
| 146 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_635 }, |
| 147 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_USBSERIAL }, |
| 148 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MX2_3 }, |
| 149 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MX4_5 }, |
| 150 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LK202 }, |
| 151 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LK204 }, |
| 152 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13M }, |
| 153 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13S }, |
| 154 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13U }, |
| 155 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_EISCOU }, |
| 156 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_UOPTBR }, |
| 157 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_EMCU2D }, |
| 158 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_PCMSFU }, |
| 159 | { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_EMCU2H }, |
| 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_SIIG2, USB_PRODUCT_SIIG2_US2308 }, |
| 164 | { 0, 0 } |
| 165 | }; |
| 166 | |
| 167 | static int |
| 168 | uftdi_match(device_t self) |
| 169 | { |
| 170 | struct usb_attach_arg *uaa = device_get_ivars(self); |
| 171 | usbd_status err; |
| 172 | u_int8_t nifaces; |
| 173 | |
| 174 | if (usb_lookup(uftdi_devs, uaa->vendor, uaa->product) == NULL) |
| 175 | return (UMATCH_NONE); |
| 176 | |
| 177 | /* Get the number of interfaces. */ |
| 178 | if (uaa->iface != NULL) { |
| 179 | nifaces = uaa->nifaces; |
| 180 | } else { |
| 181 | err = usbd_set_config_index(uaa->device, UFTDI_CONFIG_INDEX, 1); |
| 182 | if (err) |
| 183 | return (UMATCH_NONE); |
| 184 | err = usbd_interface_count(uaa->device, &nifaces); |
| 185 | if (err) |
| 186 | return (UMATCH_NONE); |
| 187 | usbd_set_config_index(uaa->device, USB_UNCONFIG_INDEX, 1); |
| 188 | } |
| 189 | |
| 190 | if (nifaces <= 1) |
| 191 | return (UMATCH_VENDOR_PRODUCT); |
| 192 | |
| 193 | /* Dual UART chip */ |
| 194 | if (uaa->iface != NULL) |
| 195 | return (UMATCH_VENDOR_IFACESUBCLASS); |
| 196 | else |
| 197 | return (UMATCH_NONE); |
| 198 | } |
| 199 | |
| 200 | static int |
| 201 | uftdi_attach(device_t self) |
| 202 | { |
| 203 | struct uftdi_softc *sc = device_get_softc(self); |
| 204 | struct usb_attach_arg *uaa = device_get_ivars(self); |
| 205 | usbd_device_handle dev = uaa->device; |
| 206 | usbd_interface_handle iface; |
| 207 | usb_interface_descriptor_t *id; |
| 208 | usb_endpoint_descriptor_t *ed; |
| 209 | char *devinfo; |
| 210 | int i; |
| 211 | usbd_status err; |
| 212 | struct ucom_softc *ucom = &sc->sc_ucom; |
| 213 | DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc)); |
| 214 | devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT); |
| 215 | |
| 216 | ucom->sc_dev = self; |
| 217 | ucom->sc_udev = dev; |
| 218 | |
| 219 | usbd_devinfo(dev, 0, devinfo); |
| 220 | device_printf(ucom->sc_dev, "%s\n", devinfo); |
| 221 | kfree(devinfo, M_USBDEV); |
| 222 | |
| 223 | if (uaa->iface == NULL) { |
| 224 | /* Move the device into the configured state. */ |
| 225 | err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1); |
| 226 | if (err) { |
| 227 | device_printf(ucom->sc_dev, |
| 228 | "failed to set configuration, err=%s\n", |
| 229 | usbd_errstr(err)); |
| 230 | goto bad; |
| 231 | } |
| 232 | |
| 233 | err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface); |
| 234 | if (err) { |
| 235 | device_printf(ucom->sc_dev, |
| 236 | "failed to get interface, err=%s\n", usbd_errstr(err)); |
| 237 | goto bad; |
| 238 | } |
| 239 | } else { |
| 240 | iface = uaa->iface; |
| 241 | } |
| 242 | |
| 243 | id = usbd_get_interface_descriptor(iface); |
| 244 | ucom->sc_iface = iface; |
| 245 | |
| 246 | if (uaa->release < 0x0200) { |
| 247 | sc->sc_type = UFTDI_TYPE_SIO; |
| 248 | sc->sc_hdrlen = 1; |
| 249 | } else { |
| 250 | sc->sc_type = UFTDI_TYPE_8U232AM; |
| 251 | sc->sc_hdrlen = 0; |
| 252 | } |
| 253 | |
| 254 | ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1; |
| 255 | |
| 256 | for (i = 0; i < id->bNumEndpoints; i++) { |
| 257 | int addr, dir, attr; |
| 258 | ed = usbd_interface2endpoint_descriptor(iface, i); |
| 259 | if (ed == NULL) { |
| 260 | device_printf(ucom->sc_dev, |
| 261 | "could not read endpoint descriptor\n"); |
| 262 | goto bad; |
| 263 | } |
| 264 | |
| 265 | addr = ed->bEndpointAddress; |
| 266 | dir = UE_GET_DIR(ed->bEndpointAddress); |
| 267 | attr = ed->bmAttributes & UE_XFERTYPE; |
| 268 | if (dir == UE_DIR_IN && attr == UE_BULK) |
| 269 | ucom->sc_bulkin_no = addr; |
| 270 | else if (dir == UE_DIR_OUT && attr == UE_BULK) |
| 271 | ucom->sc_bulkout_no = addr; |
| 272 | else { |
| 273 | device_printf(ucom->sc_dev, "unexpected endpoint\n"); |
| 274 | goto bad; |
| 275 | } |
| 276 | } |
| 277 | if (ucom->sc_bulkin_no == -1) { |
| 278 | device_printf(ucom->sc_dev, "Could not find data bulk in\n"); |
| 279 | goto bad; |
| 280 | } |
| 281 | if (ucom->sc_bulkout_no == -1) { |
| 282 | device_printf(ucom->sc_dev, "Could not find data bulk out\n"); |
| 283 | goto bad; |
| 284 | } |
| 285 | ucom->sc_parent = sc; |
| 286 | if (uaa->iface == NULL) |
| 287 | ucom->sc_portno = FTDI_PIT_SIOA; |
| 288 | else |
| 289 | ucom->sc_portno = FTDI_PIT_SIOA + id->bInterfaceNumber; |
| 290 | /* bulkin, bulkout set above */ |
| 291 | |
| 292 | ucom->sc_ibufsize = UFTDIIBUFSIZE; |
| 293 | ucom->sc_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen; |
| 294 | ucom->sc_ibufsizepad = UFTDIIBUFSIZE; |
| 295 | ucom->sc_opkthdrlen = sc->sc_hdrlen; |
| 296 | |
| 297 | |
| 298 | ucom->sc_callback = &uftdi_callback; |
| 299 | #if 0 |
| 300 | usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev, |
| 301 | ucom->sc_dev); |
| 302 | #endif |
| 303 | DPRINTF(("uftdi: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no)); |
| 304 | ucom_attach(&sc->sc_ucom); |
| 305 | return 0; |
| 306 | |
| 307 | bad: |
| 308 | DPRINTF(("uftdi_attach: ATTACH ERROR\n")); |
| 309 | ucom->sc_dying = 1; |
| 310 | return ENXIO; |
| 311 | } |
| 312 | #if 0 |
| 313 | int |
| 314 | uftdi_activate(device_t self, enum devact act) |
| 315 | { |
| 316 | struct uftdi_softc *sc = (struct uftdi_softc *)self; |
| 317 | int rv = 0; |
| 318 | |
| 319 | switch (act) { |
| 320 | case DVACT_ACTIVATE: |
| 321 | return (EOPNOTSUPP); |
| 322 | |
| 323 | case DVACT_DEACTIVATE: |
| 324 | if (sc->sc_subdev != NULL) |
| 325 | rv = config_deactivate(sc->sc_subdev); |
| 326 | sc->sc_ucom.sc_dying = 1; |
| 327 | break; |
| 328 | } |
| 329 | return (rv); |
| 330 | } |
| 331 | #endif |
| 332 | |
| 333 | static int |
| 334 | uftdi_detach(device_t self) |
| 335 | { |
| 336 | struct uftdi_softc *sc = device_get_softc(self); |
| 337 | |
| 338 | int rv = 0; |
| 339 | |
| 340 | DPRINTF(("uftdi_detach: sc=%p\n", sc)); |
| 341 | sc->sc_ucom.sc_dying = 1; |
| 342 | rv = ucom_detach(&sc->sc_ucom); |
| 343 | |
| 344 | return rv; |
| 345 | } |
| 346 | |
| 347 | static int |
| 348 | uftdi_open(void *vsc, int portno) |
| 349 | { |
| 350 | struct uftdi_softc *sc = vsc; |
| 351 | struct ucom_softc *ucom = &sc->sc_ucom; |
| 352 | usb_device_request_t req; |
| 353 | usbd_status err; |
| 354 | struct termios t; |
| 355 | |
| 356 | DPRINTF(("uftdi_open: sc=%p\n", sc)); |
| 357 | |
| 358 | if (ucom->sc_dying) |
| 359 | return (EIO); |
| 360 | |
| 361 | /* Perform a full reset on the device */ |
| 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, portno); |
| 366 | USETW(req.wLength, 0); |
| 367 | err = usbd_do_request(ucom->sc_udev, &req, NULL); |
| 368 | if (err) |
| 369 | return (EIO); |
| 370 | |
| 371 | /* Set 9600 baud, 2 stop bits, no parity, 8 bits */ |
| 372 | t.c_ospeed = 9600; |
| 373 | t.c_cflag = CSTOPB | CS8; |
| 374 | (void)uftdi_param(sc, portno, &t); |
| 375 | |
| 376 | /* Turn on RTS/CTS flow control */ |
| 377 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 378 | req.bRequest = FTDI_SIO_SET_FLOW_CTRL; |
| 379 | USETW(req.wValue, 0); |
| 380 | USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno); |
| 381 | USETW(req.wLength, 0); |
| 382 | err = usbd_do_request(ucom->sc_udev, &req, NULL); |
| 383 | if (err) |
| 384 | return (EIO); |
| 385 | |
| 386 | return (0); |
| 387 | } |
| 388 | |
| 389 | static void |
| 390 | uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count) |
| 391 | { |
| 392 | struct uftdi_softc *sc = vsc; |
| 393 | u_char msr, lsr; |
| 394 | |
| 395 | DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno, |
| 396 | *count)); |
| 397 | |
| 398 | msr = FTDI_GET_MSR(*ptr); |
| 399 | lsr = FTDI_GET_LSR(*ptr); |
| 400 | |
| 401 | #ifdef USB_DEBUG |
| 402 | if (*count != 2) |
| 403 | DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]=" |
| 404 | "0x%02x\n", sc, portno, *count, (*ptr)[2])); |
| 405 | #endif |
| 406 | |
| 407 | if (sc->sc_msr != msr || |
| 408 | (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) { |
| 409 | DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) " |
| 410 | "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr, |
| 411 | lsr, sc->sc_lsr)); |
| 412 | sc->sc_msr = msr; |
| 413 | sc->sc_lsr = lsr; |
| 414 | ucom_status_change(&sc->sc_ucom); |
| 415 | } |
| 416 | |
| 417 | /* Pick up status and adjust data part. */ |
| 418 | *ptr += 2; |
| 419 | *count -= 2; |
| 420 | } |
| 421 | |
| 422 | static void |
| 423 | uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count) |
| 424 | { |
| 425 | struct uftdi_softc *sc = vsc; |
| 426 | |
| 427 | DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n", |
| 428 | vsc, portno, *count, from[0])); |
| 429 | |
| 430 | /* Make length tag and copy data */ |
| 431 | if (sc->sc_hdrlen > 0) |
| 432 | *to = FTDI_OUT_TAG(*count, portno); |
| 433 | |
| 434 | memcpy(to + sc->sc_hdrlen, from, *count); |
| 435 | *count += sc->sc_hdrlen; |
| 436 | } |
| 437 | |
| 438 | static void |
| 439 | uftdi_set(void *vsc, int portno, int reg, int onoff) |
| 440 | { |
| 441 | struct uftdi_softc *sc = vsc; |
| 442 | struct ucom_softc *ucom = vsc; |
| 443 | usb_device_request_t req; |
| 444 | int ctl; |
| 445 | |
| 446 | DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno, |
| 447 | reg, onoff)); |
| 448 | |
| 449 | switch (reg) { |
| 450 | case UCOM_SET_DTR: |
| 451 | ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW; |
| 452 | break; |
| 453 | case UCOM_SET_RTS: |
| 454 | ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW; |
| 455 | break; |
| 456 | case UCOM_SET_BREAK: |
| 457 | uftdi_break(sc, portno, onoff); |
| 458 | return; |
| 459 | default: |
| 460 | return; |
| 461 | } |
| 462 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 463 | req.bRequest = FTDI_SIO_MODEM_CTRL; |
| 464 | USETW(req.wValue, ctl); |
| 465 | USETW(req.wIndex, portno); |
| 466 | USETW(req.wLength, 0); |
| 467 | DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x " |
| 468 | "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, |
| 469 | UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); |
| 470 | (void)usbd_do_request(ucom->sc_udev, &req, NULL); |
| 471 | } |
| 472 | |
| 473 | static int |
| 474 | uftdi_param(void *vsc, int portno, struct termios *t) |
| 475 | { |
| 476 | struct uftdi_softc *sc = vsc; |
| 477 | struct ucom_softc *ucom = &sc->sc_ucom; |
| 478 | usb_device_request_t req; |
| 479 | usbd_status err; |
| 480 | int rate=0, data, flow; |
| 481 | |
| 482 | DPRINTF(("uftdi_param: sc=%p\n", sc)); |
| 483 | |
| 484 | if (ucom->sc_dying) |
| 485 | return (EIO); |
| 486 | |
| 487 | switch (sc->sc_type) { |
| 488 | case UFTDI_TYPE_SIO: |
| 489 | switch (t->c_ospeed) { |
| 490 | case 300: rate = ftdi_sio_b300; break; |
| 491 | case 600: rate = ftdi_sio_b600; break; |
| 492 | case 1200: rate = ftdi_sio_b1200; break; |
| 493 | case 2400: rate = ftdi_sio_b2400; break; |
| 494 | case 4800: rate = ftdi_sio_b4800; break; |
| 495 | case 9600: rate = ftdi_sio_b9600; break; |
| 496 | case 19200: rate = ftdi_sio_b19200; break; |
| 497 | case 38400: rate = ftdi_sio_b38400; break; |
| 498 | case 57600: rate = ftdi_sio_b57600; break; |
| 499 | case 115200: rate = ftdi_sio_b115200; break; |
| 500 | default: |
| 501 | return (EINVAL); |
| 502 | } |
| 503 | break; |
| 504 | |
| 505 | case UFTDI_TYPE_8U232AM: |
| 506 | switch(t->c_ospeed) { |
| 507 | case 300: rate = ftdi_8u232am_b300; break; |
| 508 | case 600: rate = ftdi_8u232am_b600; break; |
| 509 | case 1200: rate = ftdi_8u232am_b1200; break; |
| 510 | case 2400: rate = ftdi_8u232am_b2400; break; |
| 511 | case 4800: rate = ftdi_8u232am_b4800; break; |
| 512 | case 9600: rate = ftdi_8u232am_b9600; break; |
| 513 | case 19200: rate = ftdi_8u232am_b19200; break; |
| 514 | case 38400: rate = ftdi_8u232am_b38400; break; |
| 515 | case 57600: rate = ftdi_8u232am_b57600; break; |
| 516 | case 115200: rate = ftdi_8u232am_b115200; break; |
| 517 | case 230400: rate = ftdi_8u232am_b230400; break; |
| 518 | case 460800: rate = ftdi_8u232am_b460800; break; |
| 519 | case 921600: rate = ftdi_8u232am_b921600; break; |
| 520 | case 2000000: rate = ftdi_8u232am_b2000000; break; |
| 521 | case 3000000: rate = ftdi_8u232am_b3000000; break; |
| 522 | default: |
| 523 | return (EINVAL); |
| 524 | } |
| 525 | break; |
| 526 | } |
| 527 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 528 | req.bRequest = FTDI_SIO_SET_BAUD_RATE; |
| 529 | USETW(req.wValue, rate); |
| 530 | USETW(req.wIndex, portno); |
| 531 | USETW(req.wLength, 0); |
| 532 | DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x " |
| 533 | "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, |
| 534 | UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); |
| 535 | err = usbd_do_request(ucom->sc_udev, &req, NULL); |
| 536 | if (err) |
| 537 | return (EIO); |
| 538 | |
| 539 | if (ISSET(t->c_cflag, CSTOPB)) |
| 540 | data = FTDI_SIO_SET_DATA_STOP_BITS_2; |
| 541 | else |
| 542 | data = FTDI_SIO_SET_DATA_STOP_BITS_1; |
| 543 | if (ISSET(t->c_cflag, PARENB)) { |
| 544 | if (ISSET(t->c_cflag, PARODD)) |
| 545 | data |= FTDI_SIO_SET_DATA_PARITY_ODD; |
| 546 | else |
| 547 | data |= FTDI_SIO_SET_DATA_PARITY_EVEN; |
| 548 | } else |
| 549 | data |= FTDI_SIO_SET_DATA_PARITY_NONE; |
| 550 | switch (ISSET(t->c_cflag, CSIZE)) { |
| 551 | case CS5: |
| 552 | data |= FTDI_SIO_SET_DATA_BITS(5); |
| 553 | break; |
| 554 | case CS6: |
| 555 | data |= FTDI_SIO_SET_DATA_BITS(6); |
| 556 | break; |
| 557 | case CS7: |
| 558 | data |= FTDI_SIO_SET_DATA_BITS(7); |
| 559 | break; |
| 560 | case CS8: |
| 561 | data |= FTDI_SIO_SET_DATA_BITS(8); |
| 562 | break; |
| 563 | } |
| 564 | sc->last_lcr = data; |
| 565 | |
| 566 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 567 | req.bRequest = FTDI_SIO_SET_DATA; |
| 568 | USETW(req.wValue, data); |
| 569 | USETW(req.wIndex, portno); |
| 570 | USETW(req.wLength, 0); |
| 571 | DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x " |
| 572 | "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, |
| 573 | UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); |
| 574 | err = usbd_do_request(ucom->sc_udev, &req, NULL); |
| 575 | if (err) |
| 576 | return (EIO); |
| 577 | |
| 578 | if (ISSET(t->c_cflag, CRTSCTS)) { |
| 579 | flow = FTDI_SIO_RTS_CTS_HS; |
| 580 | USETW(req.wValue, 0); |
| 581 | } else if (ISSET(t->c_iflag, IXON|IXOFF)) { |
| 582 | flow = FTDI_SIO_XON_XOFF_HS; |
| 583 | USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]); |
| 584 | } else { |
| 585 | flow = FTDI_SIO_DISABLE_FLOW_CTRL; |
| 586 | USETW(req.wValue, 0); |
| 587 | } |
| 588 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 589 | req.bRequest = FTDI_SIO_SET_FLOW_CTRL; |
| 590 | USETW2(req.wIndex, flow, portno); |
| 591 | USETW(req.wLength, 0); |
| 592 | err = usbd_do_request(ucom->sc_udev, &req, NULL); |
| 593 | if (err) |
| 594 | return (EIO); |
| 595 | |
| 596 | return (0); |
| 597 | } |
| 598 | |
| 599 | void |
| 600 | uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr) |
| 601 | { |
| 602 | struct uftdi_softc *sc = vsc; |
| 603 | |
| 604 | DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n", |
| 605 | sc->sc_msr, sc->sc_lsr)); |
| 606 | |
| 607 | if (msr != NULL) |
| 608 | *msr = sc->sc_msr; |
| 609 | if (lsr != NULL) |
| 610 | *lsr = sc->sc_lsr; |
| 611 | } |
| 612 | |
| 613 | void |
| 614 | uftdi_break(void *vsc, int portno, int onoff) |
| 615 | { |
| 616 | struct uftdi_softc *sc = vsc; |
| 617 | struct ucom_softc *ucom = vsc; |
| 618 | |
| 619 | usb_device_request_t req; |
| 620 | int data; |
| 621 | |
| 622 | DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno, |
| 623 | onoff)); |
| 624 | |
| 625 | if (onoff) { |
| 626 | data = sc->last_lcr | FTDI_SIO_SET_BREAK; |
| 627 | } else { |
| 628 | data = sc->last_lcr; |
| 629 | } |
| 630 | |
| 631 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
| 632 | req.bRequest = FTDI_SIO_SET_DATA; |
| 633 | USETW(req.wValue, data); |
| 634 | USETW(req.wIndex, portno); |
| 635 | USETW(req.wLength, 0); |
| 636 | (void)usbd_do_request(ucom->sc_udev, &req, NULL); |
| 637 | } |
| 638 | |
| 639 | static device_method_t uftdi_methods[] = { |
| 640 | /* Device interface */ |
| 641 | DEVMETHOD(device_probe, uftdi_match), |
| 642 | DEVMETHOD(device_attach, uftdi_attach), |
| 643 | DEVMETHOD(device_detach, uftdi_detach), |
| 644 | |
| 645 | { 0, 0 } |
| 646 | }; |
| 647 | |
| 648 | static driver_t uftdi_driver = { |
| 649 | "ucom", |
| 650 | uftdi_methods, |
| 651 | sizeof (struct uftdi_softc) |
| 652 | }; |
| 653 | |
| 654 | DRIVER_MODULE(uftdi, uhub, uftdi_driver, ucom_devclass, usbd_driver_load, 0); |
| 655 | MODULE_DEPEND(uftdi, usb, 1, 1, 1); |
| 656 | MODULE_DEPEND(uftdi, ucom,UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER); |