1 /* $NetBSD: uftdi.c,v 1.6 2001/01/23 21:56:17 augustss 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/param.h> 49 #include <sys/systm.h> 50 #include <sys/kernel.h> 51 #include <sys/device.h> 52 #include <sys/conf.h> 53 #include <sys/tty.h> 54 55 #include <dev/usb/usb.h> 56 #include <dev/usb/usbhid.h> 57 58 #include <dev/usb/usbdi.h> 59 #include <dev/usb/usbdi_util.h> 60 #include <dev/usb/usbdevs.h> 61 62 #include <dev/usb/ucomvar.h> 63 64 #include <dev/usb/uftdireg.h> 65 66 #ifdef UFTDI_DEBUG 67 #define DPRINTF(x) if (uftdidebug) printf x 68 #define DPRINTFN(n,x) if (uftdidebug>(n)) printf x 69 int uftdidebug = 0; 70 #else 71 #define DPRINTF(x) 72 #define DPRINTFN(n,x) 73 #endif 74 75 #define UFTDI_CONFIG_INDEX 0 76 #define UFTDI_IFACE_INDEX 0 77 78 79 /* 80 * These are the maximum number of bytes transferred per frame. 81 * The output buffer size cannot be increased due to the size encoding. 82 */ 83 #define UFTDIIBUFSIZE 64 84 #define UFTDIOBUFSIZE 64 85 86 struct uftdi_softc { 87 USBBASEDEVICE sc_dev; /* base device */ 88 usbd_device_handle sc_udev; /* device */ 89 usbd_interface_handle sc_iface; /* interface */ 90 91 u_char sc_msr; 92 u_char sc_lsr; 93 94 device_ptr_t sc_subdev; 95 96 u_char sc_dying; 97 }; 98 99 Static void uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr); 100 Static void uftdi_set(void *, int, int, int); 101 Static int uftdi_param(void *, int, struct termios *); 102 Static int uftdi_open(void *sc, int portno); 103 Static void uftdi_read(void *sc, int portno, u_char **ptr,u_int32_t *count); 104 Static void uftdi_write(void *sc, int portno, u_char *to, u_char *from, 105 u_int32_t *count); 106 107 struct ucom_methods uftdi_methods = { 108 uftdi_get_status, 109 uftdi_set, 110 uftdi_param, 111 NULL, 112 uftdi_open, 113 NULL, 114 uftdi_read, 115 uftdi_write, 116 }; 117 118 USB_DECLARE_DRIVER(uftdi); 119 120 USB_MATCH(uftdi) 121 { 122 USB_MATCH_START(uftdi, uaa); 123 124 if (uaa->iface != NULL) 125 return (UMATCH_NONE); 126 127 DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n", 128 uaa->vendor, uaa->product)); 129 130 if (uaa->vendor == USB_VENDOR_FTDI && 131 uaa->product == USB_PRODUCT_FTDI_SERIAL) 132 return (UMATCH_VENDOR_PRODUCT); 133 134 return (UMATCH_NONE); 135 } 136 137 USB_ATTACH(uftdi) 138 { 139 USB_ATTACH_START(uftdi, sc, uaa); 140 usbd_device_handle dev = uaa->device; 141 usbd_interface_handle iface; 142 usb_interface_descriptor_t *id; 143 usb_endpoint_descriptor_t *ed; 144 char devinfo[1024]; 145 char *devname = USBDEVNAME(sc->sc_dev); 146 int i; 147 usbd_status err; 148 struct ucom_attach_args uca; 149 150 DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc)); 151 152 /* Move the device into the configured state. */ 153 err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1); 154 if (err) { 155 printf("\n%s: failed to set configuration, err=%s\n", 156 devname, usbd_errstr(err)); 157 goto bad; 158 } 159 160 err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface); 161 if (err) { 162 printf("\n%s: failed to get interface, err=%s\n", 163 devname, usbd_errstr(err)); 164 goto bad; 165 } 166 167 usbd_devinfo(dev, 0, devinfo); 168 USB_ATTACH_SETUP; 169 printf("%s: %s\n", devname, devinfo); 170 171 id = usbd_get_interface_descriptor(iface); 172 173 sc->sc_udev = dev; 174 sc->sc_iface = iface; 175 176 uca.bulkin = uca.bulkout = -1; 177 for (i = 0; i < id->bNumEndpoints; i++) { 178 int addr, dir, attr; 179 ed = usbd_interface2endpoint_descriptor(iface, i); 180 if (ed == NULL) { 181 printf("%s: could not read endpoint descriptor" 182 ": %s\n", devname, usbd_errstr(err)); 183 goto bad; 184 } 185 186 addr = ed->bEndpointAddress; 187 dir = UE_GET_DIR(ed->bEndpointAddress); 188 attr = ed->bmAttributes & UE_XFERTYPE; 189 if (dir == UE_DIR_IN && attr == UE_BULK) 190 uca.bulkin = addr; 191 else if (dir == UE_DIR_OUT && attr == UE_BULK) 192 uca.bulkout = addr; 193 else { 194 printf("%s: unexpected endpoint\n", devname); 195 goto bad; 196 } 197 } 198 if (uca.bulkin == -1) { 199 printf("%s: Could not find data bulk in\n", 200 USBDEVNAME(sc->sc_dev)); 201 goto bad; 202 } 203 if (uca.bulkout == -1) { 204 printf("%s: Could not find data bulk out\n", 205 USBDEVNAME(sc->sc_dev)); 206 goto bad; 207 } 208 209 uca.portno = FTDI_PIT_SIOA; 210 /* bulkin, bulkout set above */ 211 uca.ibufsize = UFTDIIBUFSIZE; 212 uca.obufsize = UFTDIOBUFSIZE - 1; 213 uca.ibufsizepad = UFTDIIBUFSIZE; 214 uca.opkthdrlen = 1; 215 uca.device = dev; 216 uca.iface = iface; 217 uca.methods = &uftdi_methods; 218 uca.arg = sc; 219 uca.info = NULL; 220 221 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 222 USBDEV(sc->sc_dev)); 223 224 DPRINTF(("uftdi: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout)); 225 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 226 227 USB_ATTACH_SUCCESS_RETURN; 228 229 bad: 230 DPRINTF(("uftdi_attach: ATTACH ERROR\n")); 231 sc->sc_dying = 1; 232 USB_ATTACH_ERROR_RETURN; 233 } 234 235 int 236 uftdi_activate(device_ptr_t self, enum devact act) 237 { 238 struct uftdi_softc *sc = (struct uftdi_softc *)self; 239 int rv = 0; 240 241 switch (act) { 242 case DVACT_ACTIVATE: 243 return (EOPNOTSUPP); 244 break; 245 246 case DVACT_DEACTIVATE: 247 if (sc->sc_subdev != NULL) 248 rv = config_deactivate(sc->sc_subdev); 249 sc->sc_dying = 1; 250 break; 251 } 252 return (rv); 253 } 254 255 int 256 uftdi_detach(device_ptr_t self, int flags) 257 { 258 struct uftdi_softc *sc = (struct uftdi_softc *)self; 259 int rv = 0; 260 261 DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags)); 262 sc->sc_dying = 1; 263 if (sc->sc_subdev != NULL) { 264 rv = config_detach(sc->sc_subdev, flags); 265 sc->sc_subdev = NULL; 266 } 267 268 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 269 USBDEV(sc->sc_dev)); 270 271 return (0); 272 } 273 274 Static int 275 uftdi_open(void *vsc, int portno) 276 { 277 struct uftdi_softc *sc = vsc; 278 usb_device_request_t req; 279 usbd_status err; 280 struct termios t; 281 282 DPRINTF(("uftdi_open: sc=%p\n", sc)); 283 284 if (sc->sc_dying) 285 return (EIO); 286 287 /* Perform a full reset on the device */ 288 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 289 req.bRequest = FTDI_SIO_RESET; 290 USETW(req.wValue, FTDI_SIO_RESET_SIO); 291 USETW(req.wIndex, portno); 292 USETW(req.wLength, 0); 293 err = usbd_do_request(sc->sc_udev, &req, NULL); 294 if (err) 295 return (EIO); 296 297 /* Set 9600 baud, 2 stop bits, no parity, 8 bits */ 298 t.c_ospeed = 9600; 299 t.c_cflag = CSTOPB | CS8; 300 (void)uftdi_param(sc, portno, &t); 301 302 /* Turn on RTS/CTS flow control */ 303 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 304 req.bRequest = FTDI_SIO_SET_FLOW_CTRL; 305 USETW(req.wValue, 0); 306 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno); 307 USETW(req.wLength, 0); 308 err = usbd_do_request(sc->sc_udev, &req, NULL); 309 if (err) 310 return (EIO); 311 312 return (0); 313 } 314 315 Static void 316 uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count) 317 { 318 struct uftdi_softc *sc = vsc; 319 u_char msr, lsr; 320 321 DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno, 322 *count)); 323 324 msr = FTDI_GET_MSR(*ptr); 325 lsr = FTDI_GET_LSR(*ptr); 326 327 #ifdef UFTDI_DEBUG 328 if (*count != 2) 329 DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]=" 330 "0x%02x\n", sc, portno, *count, (*ptr)[2])); 331 #endif 332 333 if (sc->sc_msr != msr || 334 (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) { 335 DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) " 336 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr, 337 lsr, sc->sc_lsr)); 338 sc->sc_msr = msr; 339 sc->sc_lsr = lsr; 340 ucom_status_change((struct ucom_softc *)sc->sc_subdev); 341 } 342 343 /* Pick up status and adjust data part. */ 344 *ptr += 2; 345 *count -= 2; 346 } 347 348 Static void 349 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count) 350 { 351 DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n", 352 vsc, portno, *count, from[0])); 353 354 /* Make length tag and copy data */ 355 *to = FTDI_OUT_TAG(*count, portno); 356 memcpy(to+1, from, *count); 357 ++*count; 358 } 359 360 Static void 361 uftdi_set(void *vsc, int portno, int reg, int onoff) 362 { 363 struct uftdi_softc *sc = vsc; 364 usb_device_request_t req; 365 int ctl; 366 367 DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno, 368 reg, onoff)); 369 370 switch (reg) { 371 case UCOM_SET_DTR: 372 ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW; 373 break; 374 case UCOM_SET_RTS: 375 ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW; 376 break; 377 case UCOM_SET_BREAK: 378 /* XXX how do we set break? */ 379 return; 380 default: 381 return; 382 } 383 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 384 req.bRequest = FTDI_SIO_MODEM_CTRL; 385 USETW(req.wValue, ctl); 386 USETW(req.wIndex, portno); 387 USETW(req.wLength, 0); 388 DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x " 389 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, 390 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); 391 (void)usbd_do_request(sc->sc_udev, &req, NULL); 392 } 393 394 Static int 395 uftdi_param(void *vsc, int portno, struct termios *t) 396 { 397 struct uftdi_softc *sc = vsc; 398 usb_device_request_t req; 399 usbd_status err; 400 int rate, data; 401 402 DPRINTF(("uftdi_param: sc=%p\n", sc)); 403 404 if (sc->sc_dying) 405 return (EIO); 406 407 switch (t->c_ospeed) { 408 case 300: rate = ftdi_sio_b300; break; 409 case 600: rate = ftdi_sio_b600; break; 410 case 1200: rate = ftdi_sio_b1200; break; 411 case 2400: rate = ftdi_sio_b2400; break; 412 case 4800: rate = ftdi_sio_b4800; break; 413 case 9600: rate = ftdi_sio_b9600; break; 414 case 19200: rate = ftdi_sio_b19200; break; 415 case 38400: rate = ftdi_sio_b38400; break; 416 case 57600: rate = ftdi_sio_b57600; break; 417 case 115200: rate = ftdi_sio_b115200; break; 418 default: 419 return (EINVAL); 420 } 421 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 422 req.bRequest = FTDI_SIO_SET_BAUD_RATE; 423 USETW(req.wValue, rate); 424 USETW(req.wIndex, portno); 425 USETW(req.wLength, 0); 426 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x " 427 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, 428 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); 429 err = usbd_do_request(sc->sc_udev, &req, NULL); 430 if (err) 431 return (EIO); 432 433 if (ISSET(t->c_cflag, CSTOPB)) 434 data = FTDI_SIO_SET_DATA_STOP_BITS_2; 435 else 436 data = FTDI_SIO_SET_DATA_STOP_BITS_1; 437 if (ISSET(t->c_cflag, PARENB)) { 438 if (ISSET(t->c_cflag, PARODD)) 439 data |= FTDI_SIO_SET_DATA_PARITY_ODD; 440 else 441 data |= FTDI_SIO_SET_DATA_PARITY_EVEN; 442 } else 443 data |= FTDI_SIO_SET_DATA_PARITY_NONE; 444 switch (ISSET(t->c_cflag, CSIZE)) { 445 case CS5: 446 data |= FTDI_SIO_SET_DATA_BITS(5); 447 break; 448 case CS6: 449 data |= FTDI_SIO_SET_DATA_BITS(6); 450 break; 451 case CS7: 452 data |= FTDI_SIO_SET_DATA_BITS(7); 453 break; 454 case CS8: 455 data |= FTDI_SIO_SET_DATA_BITS(8); 456 break; 457 } 458 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 459 req.bRequest = FTDI_SIO_SET_DATA; 460 USETW(req.wValue, data); 461 USETW(req.wIndex, portno); 462 USETW(req.wLength, 0); 463 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x " 464 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest, 465 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength))); 466 err = usbd_do_request(sc->sc_udev, &req, NULL); 467 if (err) 468 return (EIO); 469 470 return (0); 471 } 472 473 void 474 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr) 475 { 476 struct uftdi_softc *sc = vsc; 477 478 DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n", 479 sc->sc_msr, sc->sc_lsr)); 480 481 if (msr != NULL) 482 *msr = sc->sc_msr; 483 if (lsr != NULL) 484 *lsr = sc->sc_lsr; 485 } 486