Home | History | Annotate | Line # | Download | only in usb
uftdi.c revision 1.2
      1 /*	$NetBSD: uftdi.c,v 1.2 2000/06/01 14:28:59 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 (at) 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 = 15;
     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.obufsizepad = UFTDIOBUFSIZE;
    215 	uca.device = dev;
    216 	uca.iface = iface;
    217 	uca.methods = &uftdi_methods;
    218 	uca.arg = sc;
    219 
    220 	DPRINTF(("uftdi: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
    221 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
    222 
    223 	USB_ATTACH_SUCCESS_RETURN;
    224 
    225 bad:
    226 	DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
    227 	sc->sc_dying = 1;
    228 	USB_ATTACH_ERROR_RETURN;
    229 }
    230 
    231 int
    232 uftdi_activate(device_ptr_t self, enum devact act)
    233 {
    234 	struct uftdi_softc *sc = (struct uftdi_softc *)self;
    235 	int rv = 0;
    236 
    237 	switch (act) {
    238 	case DVACT_ACTIVATE:
    239 		return (EOPNOTSUPP);
    240 		break;
    241 
    242 	case DVACT_DEACTIVATE:
    243 		if (sc->sc_subdev != NULL)
    244 			rv = config_deactivate(sc->sc_subdev);
    245 		sc->sc_dying = 1;
    246 		break;
    247 	}
    248 	return (rv);
    249 }
    250 
    251 int
    252 uftdi_detach(device_ptr_t self, int flags)
    253 {
    254 	struct uftdi_softc *sc = (struct uftdi_softc *)self;
    255 	int rv = 0;
    256 
    257 	DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
    258 	sc->sc_dying = 1;
    259 	if (sc->sc_subdev != NULL) {
    260 		rv = config_detach(sc->sc_subdev, flags);
    261 		sc->sc_subdev = NULL;
    262 	}
    263 	return (0);
    264 }
    265 
    266 Static int
    267 uftdi_open(void *vsc, int portno)
    268 {
    269 	struct uftdi_softc *sc = vsc;
    270 	usb_device_request_t req;
    271 	usbd_status err;
    272 	struct termios t;
    273 
    274 	DPRINTF(("uftdi_open: sc=%p\n", sc));
    275 
    276 	if (sc->sc_dying)
    277 		return (EIO);
    278 
    279 	/* Perform a full reset on the device */
    280 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    281 	req.bRequest = FTDI_SIO_RESET;
    282 	USETW(req.wValue, FTDI_SIO_RESET_SIO);
    283 	USETW(req.wIndex, portno);
    284 	USETW(req.wLength, 0);
    285 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    286 	if (err)
    287 		return (EIO);
    288 
    289 	/* Set 9600 baud, 2 stop bits, no parity, 8 bits */
    290 	t.c_ospeed = 9600;
    291 	t.c_cflag = CSTOPB | CS8;
    292 	(void)uftdi_param(sc, portno, &t);
    293 
    294 	/* Turn on RTS/CTS flow control */
    295 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    296 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
    297 	USETW(req.wValue, 0);
    298 	USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
    299 	USETW(req.wLength, 0);
    300 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    301 	if (err)
    302 		return (EIO);
    303 
    304 	return (0);
    305 }
    306 
    307 Static void
    308 uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
    309 {
    310 	struct uftdi_softc *sc = vsc;
    311 	u_char msr, lsr;
    312 
    313 	DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
    314 		     *count));
    315 
    316 	msr = FTDI_GET_MSR(*ptr);
    317 	lsr = FTDI_GET_LSR(*ptr);
    318 
    319 #ifdef UFTDI_DEBUG
    320 	if (*count != 2)
    321 		DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
    322 			    "0x%02x\n", sc, portno, *count, (*ptr)[2]));
    323 #endif
    324 
    325 	if (sc->sc_msr != msr ||
    326 	    (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
    327 		DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
    328 			 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
    329 			 lsr, sc->sc_lsr));
    330 		sc->sc_msr = msr;
    331 		sc->sc_lsr = lsr;
    332 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
    333 	}
    334 
    335 	/* Pick up status and adjust data part. */
    336 	*ptr += 2;
    337 	*count -= 2;
    338 }
    339 
    340 Static void
    341 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
    342 {
    343 	DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
    344 		     vsc, portno, *count, from[0]));
    345 
    346 	/* Make length tag and copy data */
    347 	*to = FTDI_OUT_TAG(*count, portno);
    348 	memcpy(to+1, from, *count);
    349 	++*count;
    350 }
    351 
    352 Static void
    353 uftdi_set(void *vsc, int portno, int reg, int onoff)
    354 {
    355 	struct uftdi_softc *sc = vsc;
    356 	usb_device_request_t req;
    357 	int ctl;
    358 
    359 	DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
    360 		 reg, onoff));
    361 
    362 	switch (reg) {
    363 	case UCOM_SET_DTR:
    364 		ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
    365 		break;
    366 	case UCOM_SET_RTS:
    367 		ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
    368 		break;
    369 	case UCOM_SET_BREAK:
    370 		/* XXX how do we set break? */
    371 		return;
    372 	default:
    373 		return;
    374 	}
    375 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    376 	req.bRequest = FTDI_SIO_MODEM_CTRL;
    377 	USETW(req.wValue, ctl);
    378 	USETW(req.wIndex, portno);
    379 	USETW(req.wLength, 0);
    380 	DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
    381 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    382 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    383 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
    384 }
    385 
    386 Static int
    387 uftdi_param(void *vsc, int portno, struct termios *t)
    388 {
    389 	struct uftdi_softc *sc = vsc;
    390 	usb_device_request_t req;
    391 	usbd_status err;
    392 	int rate, data;
    393 
    394 	DPRINTF(("uftdi_param: sc=%p\n", sc));
    395 
    396 	if (sc->sc_dying)
    397 		return (EIO);
    398 
    399 	switch (t->c_ospeed) {
    400 	case 300: rate = ftdi_sio_b300; break;
    401 	case 600: rate = ftdi_sio_b600; break;
    402 	case 1200: rate = ftdi_sio_b1200; break;
    403 	case 2400: rate = ftdi_sio_b2400; break;
    404 	case 4800: rate = ftdi_sio_b4800; break;
    405 	case 9600: rate = ftdi_sio_b9600; break;
    406 	case 19200: rate = ftdi_sio_b19200; break;
    407 	case 38400: rate = ftdi_sio_b38400; break;
    408 	case 57600: rate = ftdi_sio_b57600; break;
    409 	case 115200: rate = ftdi_sio_b115200; break;
    410 	default:
    411 		return (EINVAL);
    412 	}
    413 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    414 	req.bRequest = FTDI_SIO_SET_BAUD_RATE;
    415 	USETW(req.wValue, rate);
    416 	USETW(req.wIndex, portno);
    417 	USETW(req.wLength, 0);
    418 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
    419 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    420 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    421 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    422 	if (err)
    423 		return (EIO);
    424 
    425 	if (ISSET(t->c_cflag, CSTOPB))
    426 		data = FTDI_SIO_SET_DATA_STOP_BITS_2;
    427 	else
    428 		data = FTDI_SIO_SET_DATA_STOP_BITS_1;
    429 	if (ISSET(t->c_cflag, PARENB)) {
    430 		if (ISSET(t->c_cflag, PARODD))
    431 			data |= FTDI_SIO_SET_DATA_PARITY_ODD;
    432 		else
    433 			data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
    434 	} else
    435 		data |= FTDI_SIO_SET_DATA_PARITY_NONE;
    436 	switch (ISSET(t->c_cflag, CSIZE)) {
    437 	case CS5:
    438 		data |= FTDI_SIO_SET_DATA_BITS(5);
    439 		break;
    440 	case CS6:
    441 		data |= FTDI_SIO_SET_DATA_BITS(6);
    442 		break;
    443 	case CS7:
    444 		data |= FTDI_SIO_SET_DATA_BITS(7);
    445 		break;
    446 	case CS8:
    447 		data |= FTDI_SIO_SET_DATA_BITS(8);
    448 		break;
    449 	}
    450 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    451 	req.bRequest = FTDI_SIO_SET_DATA;
    452 	USETW(req.wValue, data);
    453 	USETW(req.wIndex, portno);
    454 	USETW(req.wLength, 0);
    455 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
    456 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
    457 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
    458 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    459 	if (err)
    460 		return (EIO);
    461 
    462 	return (0);
    463 }
    464 
    465 void
    466 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
    467 {
    468 	struct uftdi_softc *sc = vsc;
    469 
    470 	DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
    471 		 sc->sc_msr, sc->sc_lsr));
    472 
    473 	if (msr != NULL)
    474 		*msr = sc->sc_msr;
    475 	if (lsr != NULL)
    476 		*lsr = sc->sc_lsr;
    477 }
    478