1 /* $NetBSD: uftdi.c,v 1.13 2002/09/23 05:51:23 simonb Exp $ */
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * FTDI FT8U100AX serial adapter driver
46 #include <sys/cdefs.h>
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/module.h>
54 #include <sys/ioccom.h>
55 #include <sys/fcntl.h>
60 #if __FreeBSD_version >= 500014
61 #include <sys/selinfo.h>
63 #include <sys/select.h>
66 #include <sys/sysctl.h>
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbhid.h>
71 #include <dev/usb/usbdi.h>
72 #include <dev/usb/usbdi_util.h>
75 #include <dev/usb/ucomvar.h>
77 #include <dev/usb/uftdireg.h>
80 static int uftdidebug = 0;
81 SYSCTL_NODE(_hw_usb, OID_AUTO, uftdi, CTLFLAG_RW, 0, "USB uftdi");
82 SYSCTL_INT(_hw_usb_uftdi, OID_AUTO, debug, CTLFLAG_RW,
83 &uftdidebug, 0, "uftdi debug level");
84 #define DPRINTF(x) do { \
89 #define DPRINTFN(n, x) do { \
90 if (uftdidebug > (n)) \
99 #define UFTDI_CONFIG_INDEX 0
100 #define UFTDI_IFACE_INDEX 0
104 * These are the maximum number of bytes transferred per frame.
105 * The output buffer size cannot be increased due to the size encoding.
107 #define UFTDIIBUFSIZE 64
108 #define UFTDIOBUFSIZE 64
111 struct ucom_softc sc_ucom;
113 usbd_interface_handle sc_iface; /* interface */
115 enum uftdi_type sc_type;
124 Static void uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr);
125 Static void uftdi_set(void *, int, int, int);
126 Static int uftdi_param(void *, int, struct termios *);
127 Static int uftdi_open(void *sc, int portno);
128 Static void uftdi_read(void *sc, int portno, u_char **ptr,u_int32_t *count);
129 Static void uftdi_write(void *sc, int portno, u_char *to, u_char *from,
131 Static void uftdi_break(void *sc, int portno, int onoff);
133 struct ucom_callback uftdi_callback = {
146 USB_MATCH_START(uftdi, uaa);
148 if (uaa->iface != NULL) {
149 if (uaa->vendor == USB_VENDOR_FTDI &&
150 (uaa->product == USB_PRODUCT_FTDI_SERIAL_2232C))
151 return (UMATCH_VENDOR_IFACESUBCLASS);
152 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 uaa->product == USB_PRODUCT_FTDI_SEMC_DSS20 ||
162 uaa->product == USB_PRODUCT_FTDI_CFA_631 ||
163 uaa->product == USB_PRODUCT_FTDI_CFA_632 ||
164 uaa->product == USB_PRODUCT_FTDI_CFA_633 ||
165 uaa->product == USB_PRODUCT_FTDI_CFA_634 ||
166 uaa->product == USB_PRODUCT_FTDI_USBSERIAL ||
167 uaa->product == USB_PRODUCT_FTDI_MX2_3 ||
168 uaa->product == USB_PRODUCT_FTDI_MX4_5 ||
169 uaa->product == USB_PRODUCT_FTDI_LK202 ||
170 uaa->product == USB_PRODUCT_FTDI_LK204))
171 return (UMATCH_VENDOR_PRODUCT);
172 if (uaa->vendor == USB_VENDOR_SIIG2 &&
173 (uaa->product == USB_PRODUCT_SIIG2_US2308))
174 return (UMATCH_VENDOR_PRODUCT);
175 if (uaa->vendor == USB_VENDOR_INTREPIDCS &&
176 (uaa->product == USB_PRODUCT_INTREPIDCS_VALUECAN ||
177 uaa->product == USB_PRODUCT_INTREPIDCS_NEOVI))
178 return (UMATCH_VENDOR_PRODUCT);
179 if (uaa->vendor == USB_VENDOR_BBELECTRONICS &&
180 (uaa->product == USB_PRODUCT_BBELECTRONICS_USOTL4))
181 return (UMATCH_VENDOR_PRODUCT);
183 return (UMATCH_NONE);
188 USB_ATTACH_START(uftdi, sc, uaa);
189 usbd_device_handle dev = uaa->device;
190 usbd_interface_handle iface;
191 usb_interface_descriptor_t *id;
192 usb_endpoint_descriptor_t *ed;
197 struct ucom_softc *ucom = &sc->sc_ucom;
198 DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
199 devinfo = malloc(1024, M_USBDEV, M_WAITOK);
204 devname = USBDEVNAME(ucom->sc_dev);
206 if (uaa->iface == NULL) {
207 /* Move the device into the configured state. */
208 err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
210 printf("\n%s: failed to set configuration, err=%s\n",
211 devname, usbd_errstr(err));
215 err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface);
217 printf("\n%s: failed to get interface, err=%s\n",
218 devname, usbd_errstr(err));
225 usbd_devinfo(dev, 0, devinfo);
226 /* USB_ATTACH_SETUP;*/
227 printf("%s: %s\n", devname, devinfo);
229 id = usbd_get_interface_descriptor(iface);
230 ucom->sc_iface = iface;
231 switch( uaa->vendor ){
232 case USB_VENDOR_FTDI:
233 switch( uaa->product ){
234 case USB_PRODUCT_FTDI_SERIAL_8U100AX:
235 sc->sc_type = UFTDI_TYPE_SIO;
238 case USB_PRODUCT_FTDI_SEMC_DSS20:
239 case USB_PRODUCT_FTDI_SERIAL_8U232AM:
240 case USB_PRODUCT_FTDI_SERIAL_2232C:
241 case USB_PRODUCT_FTDI_CFA_631:
242 case USB_PRODUCT_FTDI_CFA_632:
243 case USB_PRODUCT_FTDI_CFA_633:
244 case USB_PRODUCT_FTDI_CFA_634:
245 case USB_PRODUCT_FTDI_USBSERIAL:
246 case USB_PRODUCT_FTDI_MX2_3:
247 case USB_PRODUCT_FTDI_MX4_5:
248 case USB_PRODUCT_FTDI_LK202:
249 case USB_PRODUCT_FTDI_LK204:
250 sc->sc_type = UFTDI_TYPE_8U232AM;
254 default: /* Can't happen */
259 case USB_VENDOR_INTREPIDCS:
260 switch( uaa->product ){
261 case USB_PRODUCT_INTREPIDCS_VALUECAN:
262 case USB_PRODUCT_INTREPIDCS_NEOVI:
263 sc->sc_type = UFTDI_TYPE_8U232AM;
267 default: /* Can't happen */
272 case USB_VENDOR_SIIG2:
273 switch( uaa->product ){
274 case USB_PRODUCT_SIIG2_US2308:
275 sc->sc_type = UFTDI_TYPE_8U232AM;
279 default: /* Can't happen */
284 case USB_VENDOR_BBELECTRONICS:
285 switch( uaa->product ){
286 case USB_PRODUCT_BBELECTRONICS_USOTL4:
287 sc->sc_type = UFTDI_TYPE_8U232AM;
291 default: /* Can't happen */
296 default: /* Can't happen */
300 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
302 for (i = 0; i < id->bNumEndpoints; i++) {
304 ed = usbd_interface2endpoint_descriptor(iface, i);
306 printf("%s: could not read endpoint descriptor\n",
311 addr = ed->bEndpointAddress;
312 dir = UE_GET_DIR(ed->bEndpointAddress);
313 attr = ed->bmAttributes & UE_XFERTYPE;
314 if (dir == UE_DIR_IN && attr == UE_BULK)
315 ucom->sc_bulkin_no = addr;
316 else if (dir == UE_DIR_OUT && attr == UE_BULK)
317 ucom->sc_bulkout_no = addr;
319 printf("%s: unexpected endpoint\n", devname);
323 if (ucom->sc_bulkin_no == -1) {
324 printf("%s: Could not find data bulk in\n",
328 if (ucom->sc_bulkout_no == -1) {
329 printf("%s: Could not find data bulk out\n",
333 ucom->sc_parent = sc;
334 if (uaa->iface == NULL)
335 ucom->sc_portno = FTDI_PIT_SIOA;
337 ucom->sc_portno = FTDI_PIT_SIOA + id->bInterfaceNumber;
338 /* bulkin, bulkout set above */
340 ucom->sc_ibufsize = UFTDIIBUFSIZE;
341 ucom->sc_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
342 ucom->sc_ibufsizepad = UFTDIIBUFSIZE;
343 ucom->sc_opkthdrlen = sc->sc_hdrlen;
346 ucom->sc_callback = &uftdi_callback;
348 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
349 USBDEV(ucom->sc_dev));
351 DPRINTF(("uftdi: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
352 ucom_attach(&sc->sc_ucom);
353 free(devinfo, M_USBDEV);
355 USB_ATTACH_SUCCESS_RETURN;
358 DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
360 free(devinfo, M_USBDEV);
362 USB_ATTACH_ERROR_RETURN;
366 uftdi_activate(device_ptr_t self, enum devact act)
368 struct uftdi_softc *sc = (struct uftdi_softc *)self;
375 case DVACT_DEACTIVATE:
376 if (sc->sc_subdev != NULL)
377 rv = config_deactivate(sc->sc_subdev);
378 sc->sc_ucom.sc_dying = 1;
387 USB_DETACH_START(uftdi, sc);
391 DPRINTF(("uftdi_detach: sc=%p\n", sc));
392 sc->sc_ucom.sc_dying = 1;
393 rv = ucom_detach(&sc->sc_ucom);
399 uftdi_open(void *vsc, int portno)
401 struct uftdi_softc *sc = vsc;
402 struct ucom_softc *ucom = &sc->sc_ucom;
403 usb_device_request_t req;
407 DPRINTF(("uftdi_open: sc=%p\n", sc));
412 /* Perform a full reset on the device */
413 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
414 req.bRequest = FTDI_SIO_RESET;
415 USETW(req.wValue, FTDI_SIO_RESET_SIO);
416 USETW(req.wIndex, portno);
417 USETW(req.wLength, 0);
418 err = usbd_do_request(ucom->sc_udev, &req, NULL);
422 /* Set 9600 baud, 2 stop bits, no parity, 8 bits */
424 t.c_cflag = CSTOPB | CS8;
425 (void)uftdi_param(sc, portno, &t);
427 /* Turn on RTS/CTS flow control */
428 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
429 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
430 USETW(req.wValue, 0);
431 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
432 USETW(req.wLength, 0);
433 err = usbd_do_request(ucom->sc_udev, &req, NULL);
441 uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
443 struct uftdi_softc *sc = vsc;
446 DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
449 msr = FTDI_GET_MSR(*ptr);
450 lsr = FTDI_GET_LSR(*ptr);
454 DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
455 "0x%02x\n", sc, portno, *count, (*ptr)[2]));
458 if (sc->sc_msr != msr ||
459 (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
460 DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
461 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
465 ucom_status_change(&sc->sc_ucom);
468 /* Pick up status and adjust data part. */
474 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
476 struct uftdi_softc *sc = vsc;
478 DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
479 vsc, portno, *count, from[0]));
481 /* Make length tag and copy data */
482 if (sc->sc_hdrlen > 0)
483 *to = FTDI_OUT_TAG(*count, portno);
485 memcpy(to + sc->sc_hdrlen, from, *count);
486 *count += sc->sc_hdrlen;
490 uftdi_set(void *vsc, int portno, int reg, int onoff)
492 struct uftdi_softc *sc = vsc;
493 struct ucom_softc *ucom = vsc;
494 usb_device_request_t req;
497 DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
502 ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
505 ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
508 uftdi_break(sc, portno, onoff);
513 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
514 req.bRequest = FTDI_SIO_MODEM_CTRL;
515 USETW(req.wValue, ctl);
516 USETW(req.wIndex, portno);
517 USETW(req.wLength, 0);
518 DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
519 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
520 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
521 (void)usbd_do_request(ucom->sc_udev, &req, NULL);
525 uftdi_param(void *vsc, int portno, struct termios *t)
527 struct uftdi_softc *sc = vsc;
528 struct ucom_softc *ucom = &sc->sc_ucom;
529 usb_device_request_t req;
531 int rate=0, data, flow;
533 DPRINTF(("uftdi_param: sc=%p\n", sc));
538 switch (sc->sc_type) {
540 switch (t->c_ospeed) {
541 case 300: rate = ftdi_sio_b300; break;
542 case 600: rate = ftdi_sio_b600; break;
543 case 1200: rate = ftdi_sio_b1200; break;
544 case 2400: rate = ftdi_sio_b2400; break;
545 case 4800: rate = ftdi_sio_b4800; break;
546 case 9600: rate = ftdi_sio_b9600; break;
547 case 19200: rate = ftdi_sio_b19200; break;
548 case 38400: rate = ftdi_sio_b38400; break;
549 case 57600: rate = ftdi_sio_b57600; break;
550 case 115200: rate = ftdi_sio_b115200; break;
556 case UFTDI_TYPE_8U232AM:
557 switch(t->c_ospeed) {
558 case 300: rate = ftdi_8u232am_b300; break;
559 case 600: rate = ftdi_8u232am_b600; break;
560 case 1200: rate = ftdi_8u232am_b1200; break;
561 case 2400: rate = ftdi_8u232am_b2400; break;
562 case 4800: rate = ftdi_8u232am_b4800; break;
563 case 9600: rate = ftdi_8u232am_b9600; break;
564 case 19200: rate = ftdi_8u232am_b19200; break;
565 case 38400: rate = ftdi_8u232am_b38400; break;
566 case 57600: rate = ftdi_8u232am_b57600; break;
567 case 115200: rate = ftdi_8u232am_b115200; break;
568 case 230400: rate = ftdi_8u232am_b230400; break;
569 case 460800: rate = ftdi_8u232am_b460800; break;
570 case 921600: rate = ftdi_8u232am_b921600; break;
571 case 2000000: rate = ftdi_8u232am_b2000000; break;
572 case 3000000: rate = ftdi_8u232am_b3000000; break;
578 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
579 req.bRequest = FTDI_SIO_SET_BAUD_RATE;
580 USETW(req.wValue, rate);
581 USETW(req.wIndex, portno);
582 USETW(req.wLength, 0);
583 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
584 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
585 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
586 err = usbd_do_request(ucom->sc_udev, &req, NULL);
590 if (ISSET(t->c_cflag, CSTOPB))
591 data = FTDI_SIO_SET_DATA_STOP_BITS_2;
593 data = FTDI_SIO_SET_DATA_STOP_BITS_1;
594 if (ISSET(t->c_cflag, PARENB)) {
595 if (ISSET(t->c_cflag, PARODD))
596 data |= FTDI_SIO_SET_DATA_PARITY_ODD;
598 data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
600 data |= FTDI_SIO_SET_DATA_PARITY_NONE;
601 switch (ISSET(t->c_cflag, CSIZE)) {
603 data |= FTDI_SIO_SET_DATA_BITS(5);
606 data |= FTDI_SIO_SET_DATA_BITS(6);
609 data |= FTDI_SIO_SET_DATA_BITS(7);
612 data |= FTDI_SIO_SET_DATA_BITS(8);
617 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
618 req.bRequest = FTDI_SIO_SET_DATA;
619 USETW(req.wValue, data);
620 USETW(req.wIndex, portno);
621 USETW(req.wLength, 0);
622 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
623 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
624 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
625 err = usbd_do_request(ucom->sc_udev, &req, NULL);
629 if (ISSET(t->c_cflag, CRTSCTS)) {
630 flow = FTDI_SIO_RTS_CTS_HS;
631 USETW(req.wValue, 0);
632 } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
633 flow = FTDI_SIO_XON_XOFF_HS;
634 USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
636 flow = FTDI_SIO_DISABLE_FLOW_CTRL;
637 USETW(req.wValue, 0);
639 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
640 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
641 USETW2(req.wIndex, flow, portno);
642 USETW(req.wLength, 0);
643 err = usbd_do_request(ucom->sc_udev, &req, NULL);
651 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
653 struct uftdi_softc *sc = vsc;
655 DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
656 sc->sc_msr, sc->sc_lsr));
665 uftdi_break(void *vsc, int portno, int onoff)
667 struct uftdi_softc *sc = vsc;
668 struct ucom_softc *ucom = vsc;
670 usb_device_request_t req;
673 DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
677 data = sc->last_lcr | FTDI_SIO_SET_BREAK;
682 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
683 req.bRequest = FTDI_SIO_SET_DATA;
684 USETW(req.wValue, data);
685 USETW(req.wIndex, portno);
686 USETW(req.wLength, 0);
687 (void)usbd_do_request(ucom->sc_udev, &req, NULL);
690 Static device_method_t uftdi_methods[] = {
691 /* Device interface */
692 DEVMETHOD(device_probe, uftdi_match),
693 DEVMETHOD(device_attach, uftdi_attach),
694 DEVMETHOD(device_detach, uftdi_detach),
699 Static driver_t uftdi_driver = {
702 sizeof (struct uftdi_softc)
705 DRIVER_MODULE(uftdi, uhub, uftdi_driver, ucom_devclass, usbd_driver_load, 0);
706 MODULE_DEPEND(uftdi, usb, 1, 1, 1);
707 MODULE_DEPEND(uftdi, ucom,UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);