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