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