1 /* $NetBSD: uftdi.c,v 1.12 2002/07/18 14:44:10 scw Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/uftdi.c,v 1.3.2.1 2002/11/21 01:28:17 ticso Exp $ */
5 * Copyright (c) 2000 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
41 * FTDI FT8U100AX serial adapter driver
45 * XXX This driver will not support multiple serial ports.
46 * XXX The ucom layer needs to be extended first.
49 #include <sys/cdefs.h>
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
56 #include <sys/ioccom.h>
57 #include <sys/fcntl.h>
62 #if __FreeBSD_version >= 500014
63 #include <sys/selinfo.h>
65 #include <sys/select.h>
68 #include <sys/sysctl.h>
70 #include <dev/usb/usb.h>
71 #include <dev/usb/usbhid.h>
73 #include <dev/usb/usbdi.h>
74 #include <dev/usb/usbdi_util.h>
75 #include <dev/usb/usbdevs.h>
77 #include <dev/usb/ucomvar.h>
79 #include <dev/usb/uftdireg.h>
82 static int uftdidebug = 0;
83 SYSCTL_NODE(_hw_usb, OID_AUTO, uftdi, CTLFLAG_RW, 0, "USB uftdi");
84 SYSCTL_INT(_hw_usb_uftdi, OID_AUTO, debug, CTLFLAG_RW,
85 &uftdidebug, 0, "uftdi debug level");
86 #define DPRINTF(x) do { \
91 #define DPRINTFN(n, x) do { \
92 if (uftdidebug > (n)) \
101 #define UFTDI_CONFIG_INDEX 0
102 #define UFTDI_IFACE_INDEX 0
106 * These are the maximum number of bytes transferred per frame.
107 * The output buffer size cannot be increased due to the size encoding.
109 #define UFTDIIBUFSIZE 64
110 #define UFTDIOBUFSIZE 64
113 struct ucom_softc sc_ucom;
115 usbd_interface_handle sc_iface; /* interface */
117 enum uftdi_type sc_type;
128 Static void uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr);
129 Static void uftdi_set(void *, int, int, int);
130 Static int uftdi_param(void *, int, struct termios *);
131 Static int uftdi_open(void *sc, int portno);
132 Static void uftdi_read(void *sc, int portno, u_char **ptr,u_int32_t *count);
133 Static void uftdi_write(void *sc, int portno, u_char *to, u_char *from,
135 Static void uftdi_break(void *sc, int portno, int onoff);
137 struct ucom_callback uftdi_callback = {
150 USB_MATCH_START(uftdi, uaa);
152 if (uaa->iface != NULL)
153 return (UMATCH_NONE);
155 DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n",
156 uaa->vendor, uaa->product));
158 if (uaa->vendor == USB_VENDOR_FTDI &&
159 (uaa->product == USB_PRODUCT_FTDI_SERIAL_8U100AX ||
160 uaa->product == USB_PRODUCT_FTDI_SERIAL_8U232AM))
161 return (UMATCH_VENDOR_PRODUCT);
163 return (UMATCH_NONE);
168 USB_ATTACH_START(uftdi, sc, uaa);
169 usbd_device_handle dev = uaa->device;
170 usbd_interface_handle iface;
171 usb_interface_descriptor_t *id;
172 usb_endpoint_descriptor_t *ed;
177 struct ucom_softc *ucom = &sc->sc_ucom;
178 DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
179 devinfo = malloc(1024, M_USBDEV, M_WAITOK);
184 devname = USBDEVNAME(ucom->sc_dev);
186 /* Move the device into the configured state. */
187 err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
189 printf("\n%s: failed to set configuration, err=%s\n",
190 devname, usbd_errstr(err));
194 err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface);
196 printf("\n%s: failed to get interface, err=%s\n",
197 devname, usbd_errstr(err));
201 usbd_devinfo(dev, 0, devinfo);
202 /* USB_ATTACH_SETUP;*/
203 printf("%s: %s\n", devname, devinfo);
205 id = usbd_get_interface_descriptor(iface);
206 ucom->sc_iface = iface;
207 switch( uaa->product ){
208 case USB_PRODUCT_FTDI_SERIAL_8U100AX:
209 sc->sc_type = UFTDI_TYPE_SIO;
212 case USB_PRODUCT_FTDI_SERIAL_8U232AM:
213 sc->sc_type = UFTDI_TYPE_8U232AM;
217 default: /* Can't happen */
221 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
223 for (i = 0; i < id->bNumEndpoints; i++) {
225 ed = usbd_interface2endpoint_descriptor(iface, i);
227 printf("%s: could not read endpoint descriptor"
228 ": %s\n", devname, usbd_errstr(err));
232 addr = ed->bEndpointAddress;
233 dir = UE_GET_DIR(ed->bEndpointAddress);
234 attr = ed->bmAttributes & UE_XFERTYPE;
235 if (dir == UE_DIR_IN && attr == UE_BULK)
236 ucom->sc_bulkin_no = addr;
237 else if (dir == UE_DIR_OUT && attr == UE_BULK)
238 ucom->sc_bulkout_no = addr;
240 printf("%s: unexpected endpoint\n", devname);
244 if (ucom->sc_bulkin_no == -1) {
245 printf("%s: Could not find data bulk in\n",
249 if (ucom->sc_bulkout_no == -1) {
250 printf("%s: Could not find data bulk out\n",
254 ucom->sc_parent = sc;
255 ucom->sc_portno = FTDI_PIT_SIOA;
256 /* bulkin, bulkout set above */
258 ucom->sc_ibufsize = UFTDIIBUFSIZE;
259 ucom->sc_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
260 ucom->sc_ibufsizepad = UFTDIIBUFSIZE;
261 ucom->sc_opkthdrlen = sc->sc_hdrlen;
264 ucom->sc_callback = &uftdi_callback;
266 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
267 USBDEV(ucom->sc_dev));
269 DPRINTF(("uftdi: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
270 ucom_attach(&sc->sc_ucom);
271 free(devinfo, M_USBDEV);
273 USB_ATTACH_SUCCESS_RETURN;
276 DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
278 free(devinfo, M_USBDEV);
280 USB_ATTACH_ERROR_RETURN;
284 uftdi_activate(device_ptr_t self, enum devact act)
286 struct uftdi_softc *sc = (struct uftdi_softc *)self;
294 case DVACT_DEACTIVATE:
295 if (sc->sc_subdev != NULL)
296 rv = config_deactivate(sc->sc_subdev);
306 USB_DETACH_START(uftdi, sc);
310 DPRINTF(("uftdi_detach: sc=%p\n", sc));
312 rv = ucom_detach(&sc->sc_ucom);
318 uftdi_open(void *vsc, int portno)
320 struct uftdi_softc *sc = vsc;
321 struct ucom_softc *ucom = (struct ucom_softc *) vsc;
322 usb_device_request_t req;
326 DPRINTF(("uftdi_open: sc=%p\n", sc));
331 /* Perform a full reset on the device */
332 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
333 req.bRequest = FTDI_SIO_RESET;
334 USETW(req.wValue, FTDI_SIO_RESET_SIO);
335 USETW(req.wIndex, portno);
336 USETW(req.wLength, 0);
337 err = usbd_do_request(ucom->sc_udev, &req, NULL);
341 /* Set 9600 baud, 2 stop bits, no parity, 8 bits */
343 t.c_cflag = CSTOPB | CS8;
344 (void)uftdi_param(sc, portno, &t);
346 /* Turn on RTS/CTS flow control */
347 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
348 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
349 USETW(req.wValue, 0);
350 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
351 USETW(req.wLength, 0);
352 err = usbd_do_request(ucom->sc_udev, &req, NULL);
360 uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
362 struct uftdi_softc *sc = vsc;
365 DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
368 msr = FTDI_GET_MSR(*ptr);
369 lsr = FTDI_GET_LSR(*ptr);
373 DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
374 "0x%02x\n", sc, portno, *count, (*ptr)[2]));
377 if (sc->sc_msr != msr ||
378 (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
379 DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
380 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
384 ucom_status_change(&sc->sc_ucom);
387 /* Pick up status and adjust data part. */
393 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
395 struct uftdi_softc *sc = vsc;
397 DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
398 vsc, portno, *count, from[0]));
400 /* Make length tag and copy data */
401 if (sc->sc_hdrlen > 0)
402 *to = FTDI_OUT_TAG(*count, portno);
404 memcpy(to + sc->sc_hdrlen, from, *count);
405 *count += sc->sc_hdrlen;
409 uftdi_set(void *vsc, int portno, int reg, int onoff)
411 struct uftdi_softc *sc = vsc;
412 struct ucom_softc *ucom = vsc;
413 usb_device_request_t req;
416 DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
421 ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
424 ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
427 uftdi_break(sc, portno, onoff);
432 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
433 req.bRequest = FTDI_SIO_MODEM_CTRL;
434 USETW(req.wValue, ctl);
435 USETW(req.wIndex, portno);
436 USETW(req.wLength, 0);
437 DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
438 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
439 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
440 (void)usbd_do_request(ucom->sc_udev, &req, NULL);
444 uftdi_param(void *vsc, int portno, struct termios *t)
446 struct uftdi_softc *sc = vsc;
447 struct ucom_softc *ucom = vsc;
448 usb_device_request_t req;
450 int rate=0, data, flow;
452 DPRINTF(("uftdi_param: sc=%p\n", sc));
457 switch (sc->sc_type) {
459 switch (t->c_ospeed) {
460 case 300: rate = ftdi_sio_b300; break;
461 case 600: rate = ftdi_sio_b600; break;
462 case 1200: rate = ftdi_sio_b1200; break;
463 case 2400: rate = ftdi_sio_b2400; break;
464 case 4800: rate = ftdi_sio_b4800; break;
465 case 9600: rate = ftdi_sio_b9600; break;
466 case 19200: rate = ftdi_sio_b19200; break;
467 case 38400: rate = ftdi_sio_b38400; break;
468 case 57600: rate = ftdi_sio_b57600; break;
469 case 115200: rate = ftdi_sio_b115200; break;
475 case UFTDI_TYPE_8U232AM:
476 switch(t->c_ospeed) {
477 case 300: rate = ftdi_8u232am_b300; break;
478 case 600: rate = ftdi_8u232am_b600; break;
479 case 1200: rate = ftdi_8u232am_b1200; break;
480 case 2400: rate = ftdi_8u232am_b2400; break;
481 case 4800: rate = ftdi_8u232am_b4800; break;
482 case 9600: rate = ftdi_8u232am_b9600; break;
483 case 19200: rate = ftdi_8u232am_b19200; break;
484 case 38400: rate = ftdi_8u232am_b38400; break;
485 case 57600: rate = ftdi_8u232am_b57600; break;
486 case 115200: rate = ftdi_8u232am_b115200; break;
487 case 230400: rate = ftdi_8u232am_b230400; break;
488 case 460800: rate = ftdi_8u232am_b460800; break;
489 case 921600: rate = ftdi_8u232am_b921600; break;
495 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
496 req.bRequest = FTDI_SIO_SET_BAUD_RATE;
497 USETW(req.wValue, rate);
498 USETW(req.wIndex, portno);
499 USETW(req.wLength, 0);
500 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
501 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
502 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
503 err = usbd_do_request(ucom->sc_udev, &req, NULL);
507 if (ISSET(t->c_cflag, CSTOPB))
508 data = FTDI_SIO_SET_DATA_STOP_BITS_2;
510 data = FTDI_SIO_SET_DATA_STOP_BITS_1;
511 if (ISSET(t->c_cflag, PARENB)) {
512 if (ISSET(t->c_cflag, PARODD))
513 data |= FTDI_SIO_SET_DATA_PARITY_ODD;
515 data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
517 data |= FTDI_SIO_SET_DATA_PARITY_NONE;
518 switch (ISSET(t->c_cflag, CSIZE)) {
520 data |= FTDI_SIO_SET_DATA_BITS(5);
523 data |= FTDI_SIO_SET_DATA_BITS(6);
526 data |= FTDI_SIO_SET_DATA_BITS(7);
529 data |= FTDI_SIO_SET_DATA_BITS(8);
534 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
535 req.bRequest = FTDI_SIO_SET_DATA;
536 USETW(req.wValue, data);
537 USETW(req.wIndex, portno);
538 USETW(req.wLength, 0);
539 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
540 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
541 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
542 err = usbd_do_request(ucom->sc_udev, &req, NULL);
546 if (ISSET(t->c_cflag, CRTSCTS)) {
547 flow = FTDI_SIO_RTS_CTS_HS;
548 USETW(req.wValue, 0);
549 } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
550 flow = FTDI_SIO_XON_XOFF_HS;
551 USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
553 flow = FTDI_SIO_DISABLE_FLOW_CTRL;
554 USETW(req.wValue, 0);
556 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
557 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
558 USETW2(req.wIndex, flow, portno);
559 USETW(req.wLength, 0);
560 err = usbd_do_request(ucom->sc_udev, &req, NULL);
568 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
570 struct uftdi_softc *sc = vsc;
572 DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
573 sc->sc_msr, sc->sc_lsr));
582 uftdi_break(void *vsc, int portno, int onoff)
584 struct uftdi_softc *sc = vsc;
585 struct ucom_softc *ucom = vsc;
587 usb_device_request_t req;
590 DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
594 data = sc->last_lcr | FTDI_SIO_SET_BREAK;
599 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
600 req.bRequest = FTDI_SIO_SET_DATA;
601 USETW(req.wValue, data);
602 USETW(req.wIndex, portno);
603 USETW(req.wLength, 0);
604 (void)usbd_do_request(ucom->sc_udev, &req, NULL);
607 Static device_method_t uftdi_methods[] = {
608 /* Device interface */
609 DEVMETHOD(device_probe, uftdi_match),
610 DEVMETHOD(device_attach, uftdi_attach),
611 DEVMETHOD(device_detach, uftdi_detach),
616 Static driver_t uftdi_driver = {
619 sizeof (struct uftdi_softc)
622 DRIVER_MODULE(uftdi, uhub, uftdi_driver, ucom_devclass, usbd_driver_load, 0);
623 MODULE_DEPEND(uftdi, usb, 1, 1, 1);
624 MODULE_DEPEND(uftdi, ucom,UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);