1 /* $OpenBSD: uftdi.c,v 1.3 2001/05/03 02:20:33 aaron Exp $ */ 2 /* $NetBSD: uftdi.c,v 1.6 2001/01/23 21:56:17 augustss Exp $ */ 3 4 /* 5 * Copyright (c) 2000 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Lennart Augustsson (lennart@augustsson.net). 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * FTDI FT8U100AX serial adapter driver 42 */ 43 44 /* 45 * XXX This driver will not support multiple serial ports. 46 * XXX The ucom layer needs to be extended first. 47 */ 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/kernel.h> 52 #include <sys/device.h> 53 #include <sys/conf.h> 54 #include <sys/tty.h> 55 56 #include <dev/usb/usb.h> 57 #include <dev/usb/usbhid.h> 58 59 #include <dev/usb/usbdi.h> 60 #include <dev/usb/usbdi_util.h> 61 #include <dev/usb/usbdevs.h> 62 63 #include <dev/usb/ucomvar.h> 64 65 #include <dev/usb/uftdireg.h> 66 67 #ifdef UFTDI_DEBUG 68 #define DPRINTF(x) if (uftdidebug) printf x 69 #define DPRINTFN(n,x) if (uftdidebug>(n)) printf x 70 int uftdidebug = 0; 71 #else 72 #define DPRINTF(x) 73 #define DPRINTFN(n,x) 74 #endif 75 76 #define UFTDI_CONFIG_INDEX 0 77 #define UFTDI_IFACE_INDEX 0 78 79 80 /* 81 * These are the maximum number of bytes transferred per frame. 82 * The output buffer size cannot be increased due to the size encoding. 83 */ 84 #define UFTDIIBUFSIZE 64 85 #define UFTDIOBUFSIZE 64 86 87 struct uftdi_softc { 88 USBBASEDEVICE sc_dev; /* base device */ 89 usbd_device_handle sc_udev; /* device */ 90 usbd_interface_handle sc_iface; /* interface */ 91 92 u_char sc_msr; 93 u_char sc_lsr; 94 95 device_ptr_t sc_subdev; 96 97 u_char sc_dying; 98 }; 99 100 Static void uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr); 101 Static void uftdi_set(void *, int, int, int); 102 Static int uftdi_param(void *, int, struct termios *); 103 Static int uftdi_open(void *sc, int portno); 104 Static void uftdi_read(void *sc, int portno, u_char **ptr,u_int32_t *count); 105 Static void uftdi_write(void *sc, int portno, u_char *to, u_char *from, 106 u_int32_t *count); 107 108 struct ucom_methods uftdi_methods = { 109 uftdi_get_status, 110 uftdi_set, 111 uftdi_param, 112 NULL, 113 uftdi_open, 114 NULL, 115 uftdi_read, 116 uftdi_write, 117 }; 118 119 USB_DECLARE_DRIVER(uftdi); 120 121 USB_MATCH(uftdi) 122 { 123 USB_MATCH_START(uftdi, uaa); 124 125 if (uaa->iface != NULL) 126 return (UMATCH_NONE); 127 128 DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n", 129 uaa->vendor, uaa->product)); 130 131 if (uaa->vendor == USB_VENDOR_FTDI && 132 uaa->product == USB_PRODUCT_FTDI_SERIAL) 133 return (UMATCH_VENDOR_PRODUCT); 134 135 return (UMATCH_NONE); 136 } 137 138 USB_ATTACH(uftdi) 139 { 140 USB_ATTACH_START(uftdi, sc, uaa); 141 usbd_device_handle dev = uaa->device; 142 usbd_interface_handle iface; 143 usb_interface_descriptor_t *id; 144 usb_endpoint_descriptor_t *ed; 145 char devinfo[1024]; 146 char *devname = USBDEVNAME(sc->sc_dev); 147 int i; 148 usbd_status err; 149 struct ucom_attach_args uca; 150 151 DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc)); 152 153 /* Move the device into the configured state. */ 154 err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1); 155 if (err) { 156 printf("\n%s: failed to set configuration, err=%s\n", 157 devname, usbd_errstr(err)); 158 goto bad; 159 } 160 161 err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface); 162 if (err) { 163 printf("\n%s: failed to get interface, err=%s\n", 164 devname, usbd_errstr(err)); 165 goto bad; 166 } 167 168 usbd_devinfo(dev, 0, devinfo); 169 USB_ATTACH_SETUP; 170 printf("%s: %s\n", devname, devinfo); 171 172 id = usbd_get_interface_descriptor(iface); 173 174 sc->sc_udev = dev; 175 sc->sc_iface = iface; 176 177 uca.bulkin = uca.bulkout = -1; 178 for (i = 0; i < id->bNumEndpoints; i++) { 179 int addr, dir, attr; 180 ed = usbd_interface2endpoint_descriptor(iface, i); 181 if (ed == NULL) { 182 printf("%s: could not read endpoint descriptor" 183 ": %s\n", devname, usbd_errstr(err)); 184 goto bad; 185 } 186 187 addr = ed->bEndpointAddress; 188 dir = UE_GET_DIR(ed->bEndpointAddress); 189 attr = ed->bmAttributes & UE_XFERTYPE; 190 if (dir == UE_DIR_IN && attr == UE_BULK) 191 uca.bulkin = addr; 192 else if (dir == UE_DIR_OUT && attr == UE_BULK) 193 uca.bulkout = addr; 194 else { 195 printf("%s: unexpected endpoint\n", devname); 196 goto bad; 197 } 198 } 199 if (uca.bulkin == -1) { 200 printf("%s: Could not find data bulk in\n", 201 USBDEVNAME(sc->sc_dev)); 202 goto bad; 203 } 204 if (uca.bulkout == -1) { 205 printf("%s: Could not find data bulk out\n", 206 USBDEVNAME(sc->sc_dev)); 207 goto bad; 208 } 209 210 uca.portno = FTDI_PIT_SIOA; 211 /* bulkin, bulkout set above */ 212 uca.ibufsize = UFTDIIBUFSIZE; 213 uca.obufsize = UFTDIOBUFSIZE - 1; 214 uca.ibufsizepad = UFTDIIBUFSIZE; 215 uca.opkthdrlen = 1; 216 uca.device = dev; 217 uca.iface = iface; 218 uca.methods = &uftdi_methods; 219 uca.arg = sc; 220 uca.info = NULL; 221 222 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 223 USBDEV(sc->sc_dev)); 224 225 DPRINTF(("uftdi: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout)); 226 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 227 228 USB_ATTACH_SUCCESS_RETURN; 229 230 bad: 231 DPRINTF(("uftdi_attach: ATTACH ERROR\n")); 232 sc->sc_dying = 1; 233 USB_ATTACH_ERROR_RETURN; 234 } 235 236 int 237 uftdi_activate(device_ptr_t self, enum devact act) 238 { 239 struct uftdi_softc *sc = (struct uftdi_softc *)self; 240 int rv = 0; 241 242 switch (act) { 243 case DVACT_ACTIVATE: 244 return (EOPNOTSUPP); 245 break; 246 247 case DVACT_DEACTIVATE: 248 if (sc->sc_subdev != NULL) 249 rv = config_deactivate(sc->sc_subdev); 250 sc->sc_dying = 1; 251 break; 252 } 253 return (rv); 254 } 255 256 int 257 uftdi_detach(device_ptr_t self, int flags) 258 { 259 struct uftdi_softc *sc = (struct uftdi_softc *)self; 260 int rv = 0; 261 262 DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags)); 263 sc->sc_dying = 1; 264 if (sc->sc_subdev != NULL) { 265 rv = config_detach(sc->sc_subdev, flags); 266 sc->sc_subdev = NULL; 267 } 268 269 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 270 USBDEV(sc->sc_dev)); 271 272 return (0); 273 } 274 275 Static int 276 uftdi_open(void *vsc, int portno) 277 { 278 struct uftdi_softc *sc = vsc; 279 usb_device_request_t req; 280 usbd_status err; 281 struct termios t; 282 283 DPRINTF(("uftdi_open: sc=%p\n", sc)); 284 285 if (sc->sc_dying) 286 return (EIO); 287 288 /* Perform a full reset on the device */ 289 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 290 req.bRequest = FTDI_SIO_RESET; 291 USETW(req.wValue, FTDI_SIO_RESET_SIO); 292 USETW(req.wIndex, portno); 293 USETW(req.wLength, 0); 294 err = usbd_do_request(sc->sc_udev, &req, NULL); 295 if (err) 296 return (EIO); 297 298 /* Set 9600 baud, 2 stop bits, no parity, 8 bits */ 299 t.c_ospeed = 9600; 300 t.c_cflag = CSTOPB | CS8; 301 (void)uftdi_param(sc, portno, &t); 302 303 /* Turn on RTS/CTS flow control */ 304 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 305 req.bRequest = FTDI_SIO_SET_FLOW_CTRL; 306 USETW(req.wValue, 0); 307 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno); 308 USETW(req.wLength, 0); 309 err = usbd_do_request(sc->sc_udev, &req, NULL); 310 if (err) 311 return (EIO); 312 313 return (0); 314 } 315 316 Static void 317 uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count) 318 { 319 struct uftdi_softc *sc = vsc; 320 u_char msr, lsr; 321 322 DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno, 323 *count)); 324 325 msr = FTDI_GET_MSR(*ptr); 326 lsr = FTDI_GET_LSR(*ptr); 327 328 #ifdef UFTDI_DEBUG 329 if (*count != 2) 330 DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]=" 331 "0x%02x\n", sc, portno, *count, (*ptr)[2])); 332 #endif 333 334 if (sc->sc_msr != msr || 335 (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) { 336 DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) " 337 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr, 338 lsr, sc->sc_lsr)); 339 sc->sc_msr = msr; 340 sc->sc_lsr = lsr; 341 ucom_status_change((struct ucom_softc *)sc->sc_subdev); 342 } 343 344 /* Pick up status and adjust data part. */ 345 *ptr += 2; 346 *count -= 2; 347 } 348 349 Static void 350 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count) 351 { 352 DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n", 353 vsc, portno, *count, from[0])); 354 355 /* Make length tag and copy data */ 356 *to = FTDI_OUT_TAG(*count, portno); 357 memcpy(to+1, from, *count); 358 ++*count; 359 } 360 361 Static void 362 uftdi_set(void *vsc, int portno, int reg, int onoff) 363 { 364 struct uftdi_softc *sc = vsc; 365 usb_device_request_t req; 366 int ctl; 367 368 DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno, 369 reg, onoff)); 370 371 switch (reg) { 372 case UCOM_SET_DTR: 373 ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW; 374 break; 375 case UCOM_SET_RTS: 376 ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW; 377 break; 378 case UCOM_SET_BREAK: 379 /* XXX how do we set break? */ 380 return; 381 default: 382 return; 383 } 384 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 385 req.bRequest = FTDI_SIO_MODEM_CTRL; 386 USETW(req.wValue, ctl); 387 USETW(req.wIndex, portno); 388 USETW(req.wLength, 0); 389 DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x " 390 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, 391 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); 392 (void)usbd_do_request(sc->sc_udev, &req, NULL); 393 } 394 395 Static int 396 uftdi_param(void *vsc, int portno, struct termios *t) 397 { 398 struct uftdi_softc *sc = vsc; 399 usb_device_request_t req; 400 usbd_status err; 401 int rate, data; 402 403 DPRINTF(("uftdi_param: sc=%p\n", sc)); 404 405 if (sc->sc_dying) 406 return (EIO); 407 408 switch (t->c_ospeed) { 409 case 300: rate = ftdi_sio_b300; break; 410 case 600: rate = ftdi_sio_b600; break; 411 case 1200: rate = ftdi_sio_b1200; break; 412 case 2400: rate = ftdi_sio_b2400; break; 413 case 4800: rate = ftdi_sio_b4800; break; 414 case 9600: rate = ftdi_sio_b9600; break; 415 case 19200: rate = ftdi_sio_b19200; break; 416 case 38400: rate = ftdi_sio_b38400; break; 417 case 57600: rate = ftdi_sio_b57600; break; 418 case 115200: rate = ftdi_sio_b115200; break; 419 default: 420 return (EINVAL); 421 } 422 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 423 req.bRequest = FTDI_SIO_SET_BAUD_RATE; 424 USETW(req.wValue, rate); 425 USETW(req.wIndex, portno); 426 USETW(req.wLength, 0); 427 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x " 428 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, 429 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); 430 err = usbd_do_request(sc->sc_udev, &req, NULL); 431 if (err) 432 return (EIO); 433 434 if (ISSET(t->c_cflag, CSTOPB)) 435 data = FTDI_SIO_SET_DATA_STOP_BITS_2; 436 else 437 data = FTDI_SIO_SET_DATA_STOP_BITS_1; 438 if (ISSET(t->c_cflag, PARENB)) { 439 if (ISSET(t->c_cflag, PARODD)) 440 data |= FTDI_SIO_SET_DATA_PARITY_ODD; 441 else 442 data |= FTDI_SIO_SET_DATA_PARITY_EVEN; 443 } else 444 data |= FTDI_SIO_SET_DATA_PARITY_NONE; 445 switch (ISSET(t->c_cflag, CSIZE)) { 446 case CS5: 447 data |= FTDI_SIO_SET_DATA_BITS(5); 448 break; 449 case CS6: 450 data |= FTDI_SIO_SET_DATA_BITS(6); 451 break; 452 case CS7: 453 data |= FTDI_SIO_SET_DATA_BITS(7); 454 break; 455 case CS8: 456 data |= FTDI_SIO_SET_DATA_BITS(8); 457 break; 458 } 459 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 460 req.bRequest = FTDI_SIO_SET_DATA; 461 USETW(req.wValue, data); 462 USETW(req.wIndex, portno); 463 USETW(req.wLength, 0); 464 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x " 465 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, 466 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); 467 err = usbd_do_request(sc->sc_udev, &req, NULL); 468 if (err) 469 return (EIO); 470 471 return (0); 472 } 473 474 void 475 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr) 476 { 477 struct uftdi_softc *sc = vsc; 478 479 DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n", 480 sc->sc_msr, sc->sc_lsr)); 481 482 if (msr != NULL) 483 *msr = sc->sc_msr; 484 if (lsr != NULL) 485 *lsr = sc->sc_lsr; 486 } 487