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 $ */
3 /* $DragonFly: src/sys/dev/usbmisc/uftdi/uftdi.c,v 1.3 2003/08/07 21:17:14 dillon Exp $ */
6 * Copyright (c) 2000 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
42 * FTDI FT8U100AX serial adapter driver
46 * XXX This driver will not support multiple serial ports.
47 * XXX The ucom layer needs to be extended first.
50 #include <sys/cdefs.h>
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
57 #include <sys/ioccom.h>
58 #include <sys/fcntl.h>
63 #if __FreeBSD_version >= 500014
64 #include <sys/selinfo.h>
66 #include <sys/select.h>
69 #include <sys/sysctl.h>
71 #include <bus/usb/usb.h>
72 #include <bus/usb/usbhid.h>
74 #include <bus/usb/usbdi.h>
75 #include <bus/usb/usbdi_util.h>
76 #include <bus/usb/usbdevs.h>
78 #include "../ucom/ucomvar.h"
83 static int uftdidebug = 0;
84 SYSCTL_NODE(_hw_usb, OID_AUTO, uftdi, CTLFLAG_RW, 0, "USB uftdi");
85 SYSCTL_INT(_hw_usb_uftdi, OID_AUTO, debug, CTLFLAG_RW,
86 &uftdidebug, 0, "uftdi debug level");
87 #define DPRINTF(x) do { \
92 #define DPRINTFN(n, x) do { \
93 if (uftdidebug > (n)) \
102 #define UFTDI_CONFIG_INDEX 0
103 #define UFTDI_IFACE_INDEX 0
107 * These are the maximum number of bytes transferred per frame.
108 * The output buffer size cannot be increased due to the size encoding.
110 #define UFTDIIBUFSIZE 64
111 #define UFTDIOBUFSIZE 64
114 struct ucom_softc sc_ucom;
116 usbd_interface_handle sc_iface; /* interface */
118 enum uftdi_type sc_type;
129 Static void uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr);
130 Static void uftdi_set(void *, int, int, int);
131 Static int uftdi_param(void *, int, struct termios *);
132 Static int uftdi_open(void *sc, int portno);
133 Static void uftdi_read(void *sc, int portno, u_char **ptr,u_int32_t *count);
134 Static void uftdi_write(void *sc, int portno, u_char *to, u_char *from,
136 Static void uftdi_break(void *sc, int portno, int onoff);
138 struct ucom_callback uftdi_callback = {
151 USB_MATCH_START(uftdi, uaa);
153 if (uaa->iface != NULL)
154 return (UMATCH_NONE);
156 DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n",
157 uaa->vendor, uaa->product));
159 if (uaa->vendor == USB_VENDOR_FTDI &&
160 (uaa->product == USB_PRODUCT_FTDI_SERIAL_8U100AX ||
161 uaa->product == USB_PRODUCT_FTDI_SERIAL_8U232AM))
162 return (UMATCH_VENDOR_PRODUCT);
164 return (UMATCH_NONE);
169 USB_ATTACH_START(uftdi, sc, uaa);
170 usbd_device_handle dev = uaa->device;
171 usbd_interface_handle iface;
172 usb_interface_descriptor_t *id;
173 usb_endpoint_descriptor_t *ed;
178 struct ucom_softc *ucom = &sc->sc_ucom;
179 DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
180 devinfo = malloc(1024, M_USBDEV, M_WAITOK);
185 devname = USBDEVNAME(ucom->sc_dev);
187 /* Move the device into the configured state. */
188 err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
190 printf("\n%s: failed to set configuration, err=%s\n",
191 devname, usbd_errstr(err));
195 err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface);
197 printf("\n%s: failed to get interface, err=%s\n",
198 devname, usbd_errstr(err));
202 usbd_devinfo(dev, 0, devinfo);
203 /* USB_ATTACH_SETUP;*/
204 printf("%s: %s\n", devname, devinfo);
206 id = usbd_get_interface_descriptor(iface);
207 ucom->sc_iface = iface;
208 switch( uaa->product ){
209 case USB_PRODUCT_FTDI_SERIAL_8U100AX:
210 sc->sc_type = UFTDI_TYPE_SIO;
213 case USB_PRODUCT_FTDI_SERIAL_8U232AM:
214 sc->sc_type = UFTDI_TYPE_8U232AM;
218 default: /* Can't happen */
222 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
224 for (i = 0; i < id->bNumEndpoints; i++) {
226 ed = usbd_interface2endpoint_descriptor(iface, i);
228 printf("%s: could not read endpoint descriptor"
229 ": %s\n", devname, usbd_errstr(err));
233 addr = ed->bEndpointAddress;
234 dir = UE_GET_DIR(ed->bEndpointAddress);
235 attr = ed->bmAttributes & UE_XFERTYPE;
236 if (dir == UE_DIR_IN && attr == UE_BULK)
237 ucom->sc_bulkin_no = addr;
238 else if (dir == UE_DIR_OUT && attr == UE_BULK)
239 ucom->sc_bulkout_no = addr;
241 printf("%s: unexpected endpoint\n", devname);
245 if (ucom->sc_bulkin_no == -1) {
246 printf("%s: Could not find data bulk in\n",
250 if (ucom->sc_bulkout_no == -1) {
251 printf("%s: Could not find data bulk out\n",
255 ucom->sc_parent = sc;
256 ucom->sc_portno = FTDI_PIT_SIOA;
257 /* bulkin, bulkout set above */
259 ucom->sc_ibufsize = UFTDIIBUFSIZE;
260 ucom->sc_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
261 ucom->sc_ibufsizepad = UFTDIIBUFSIZE;
262 ucom->sc_opkthdrlen = sc->sc_hdrlen;
265 ucom->sc_callback = &uftdi_callback;
267 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
268 USBDEV(ucom->sc_dev));
270 DPRINTF(("uftdi: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
271 ucom_attach(&sc->sc_ucom);
272 free(devinfo, M_USBDEV);
274 USB_ATTACH_SUCCESS_RETURN;
277 DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
279 free(devinfo, M_USBDEV);
281 USB_ATTACH_ERROR_RETURN;
285 uftdi_activate(device_ptr_t self, enum devact act)
287 struct uftdi_softc *sc = (struct uftdi_softc *)self;
295 case DVACT_DEACTIVATE:
296 if (sc->sc_subdev != NULL)
297 rv = config_deactivate(sc->sc_subdev);
307 USB_DETACH_START(uftdi, sc);
311 DPRINTF(("uftdi_detach: sc=%p\n", sc));
313 rv = ucom_detach(&sc->sc_ucom);
319 uftdi_open(void *vsc, int portno)
321 struct uftdi_softc *sc = vsc;
322 struct ucom_softc *ucom = (struct ucom_softc *) vsc;
323 usb_device_request_t req;
327 DPRINTF(("uftdi_open: sc=%p\n", sc));
332 /* Perform a full reset on the device */
333 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
334 req.bRequest = FTDI_SIO_RESET;
335 USETW(req.wValue, FTDI_SIO_RESET_SIO);
336 USETW(req.wIndex, portno);
337 USETW(req.wLength, 0);
338 err = usbd_do_request(ucom->sc_udev, &req, NULL);
342 /* Set 9600 baud, 2 stop bits, no parity, 8 bits */
344 t.c_cflag = CSTOPB | CS8;
345 (void)uftdi_param(sc, portno, &t);
347 /* Turn on RTS/CTS flow control */
348 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
349 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
350 USETW(req.wValue, 0);
351 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
352 USETW(req.wLength, 0);
353 err = usbd_do_request(ucom->sc_udev, &req, NULL);
361 uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
363 struct uftdi_softc *sc = vsc;
366 DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
369 msr = FTDI_GET_MSR(*ptr);
370 lsr = FTDI_GET_LSR(*ptr);
374 DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
375 "0x%02x\n", sc, portno, *count, (*ptr)[2]));
378 if (sc->sc_msr != msr ||
379 (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
380 DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
381 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
385 ucom_status_change(&sc->sc_ucom);
388 /* Pick up status and adjust data part. */
394 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
396 struct uftdi_softc *sc = vsc;
398 DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
399 vsc, portno, *count, from[0]));
401 /* Make length tag and copy data */
402 if (sc->sc_hdrlen > 0)
403 *to = FTDI_OUT_TAG(*count, portno);
405 memcpy(to + sc->sc_hdrlen, from, *count);
406 *count += sc->sc_hdrlen;
410 uftdi_set(void *vsc, int portno, int reg, int onoff)
412 struct uftdi_softc *sc = vsc;
413 struct ucom_softc *ucom = vsc;
414 usb_device_request_t req;
417 DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
422 ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
425 ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
428 uftdi_break(sc, portno, onoff);
433 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
434 req.bRequest = FTDI_SIO_MODEM_CTRL;
435 USETW(req.wValue, ctl);
436 USETW(req.wIndex, portno);
437 USETW(req.wLength, 0);
438 DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
439 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
440 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
441 (void)usbd_do_request(ucom->sc_udev, &req, NULL);
445 uftdi_param(void *vsc, int portno, struct termios *t)
447 struct uftdi_softc *sc = vsc;
448 struct ucom_softc *ucom = vsc;
449 usb_device_request_t req;
451 int rate=0, data, flow;
453 DPRINTF(("uftdi_param: sc=%p\n", sc));
458 switch (sc->sc_type) {
460 switch (t->c_ospeed) {
461 case 300: rate = ftdi_sio_b300; break;
462 case 600: rate = ftdi_sio_b600; break;
463 case 1200: rate = ftdi_sio_b1200; break;
464 case 2400: rate = ftdi_sio_b2400; break;
465 case 4800: rate = ftdi_sio_b4800; break;
466 case 9600: rate = ftdi_sio_b9600; break;
467 case 19200: rate = ftdi_sio_b19200; break;
468 case 38400: rate = ftdi_sio_b38400; break;
469 case 57600: rate = ftdi_sio_b57600; break;
470 case 115200: rate = ftdi_sio_b115200; break;
476 case UFTDI_TYPE_8U232AM:
477 switch(t->c_ospeed) {
478 case 300: rate = ftdi_8u232am_b300; break;
479 case 600: rate = ftdi_8u232am_b600; break;
480 case 1200: rate = ftdi_8u232am_b1200; break;
481 case 2400: rate = ftdi_8u232am_b2400; break;
482 case 4800: rate = ftdi_8u232am_b4800; break;
483 case 9600: rate = ftdi_8u232am_b9600; break;
484 case 19200: rate = ftdi_8u232am_b19200; break;
485 case 38400: rate = ftdi_8u232am_b38400; break;
486 case 57600: rate = ftdi_8u232am_b57600; break;
487 case 115200: rate = ftdi_8u232am_b115200; break;
488 case 230400: rate = ftdi_8u232am_b230400; break;
489 case 460800: rate = ftdi_8u232am_b460800; break;
490 case 921600: rate = ftdi_8u232am_b921600; break;
496 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
497 req.bRequest = FTDI_SIO_SET_BAUD_RATE;
498 USETW(req.wValue, rate);
499 USETW(req.wIndex, portno);
500 USETW(req.wLength, 0);
501 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
502 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
503 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
504 err = usbd_do_request(ucom->sc_udev, &req, NULL);
508 if (ISSET(t->c_cflag, CSTOPB))
509 data = FTDI_SIO_SET_DATA_STOP_BITS_2;
511 data = FTDI_SIO_SET_DATA_STOP_BITS_1;
512 if (ISSET(t->c_cflag, PARENB)) {
513 if (ISSET(t->c_cflag, PARODD))
514 data |= FTDI_SIO_SET_DATA_PARITY_ODD;
516 data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
518 data |= FTDI_SIO_SET_DATA_PARITY_NONE;
519 switch (ISSET(t->c_cflag, CSIZE)) {
521 data |= FTDI_SIO_SET_DATA_BITS(5);
524 data |= FTDI_SIO_SET_DATA_BITS(6);
527 data |= FTDI_SIO_SET_DATA_BITS(7);
530 data |= FTDI_SIO_SET_DATA_BITS(8);
535 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
536 req.bRequest = FTDI_SIO_SET_DATA;
537 USETW(req.wValue, data);
538 USETW(req.wIndex, portno);
539 USETW(req.wLength, 0);
540 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
541 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
542 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
543 err = usbd_do_request(ucom->sc_udev, &req, NULL);
547 if (ISSET(t->c_cflag, CRTSCTS)) {
548 flow = FTDI_SIO_RTS_CTS_HS;
549 USETW(req.wValue, 0);
550 } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
551 flow = FTDI_SIO_XON_XOFF_HS;
552 USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
554 flow = FTDI_SIO_DISABLE_FLOW_CTRL;
555 USETW(req.wValue, 0);
557 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
558 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
559 USETW2(req.wIndex, flow, portno);
560 USETW(req.wLength, 0);
561 err = usbd_do_request(ucom->sc_udev, &req, NULL);
569 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
571 struct uftdi_softc *sc = vsc;
573 DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
574 sc->sc_msr, sc->sc_lsr));
583 uftdi_break(void *vsc, int portno, int onoff)
585 struct uftdi_softc *sc = vsc;
586 struct ucom_softc *ucom = vsc;
588 usb_device_request_t req;
591 DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
595 data = sc->last_lcr | FTDI_SIO_SET_BREAK;
600 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
601 req.bRequest = FTDI_SIO_SET_DATA;
602 USETW(req.wValue, data);
603 USETW(req.wIndex, portno);
604 USETW(req.wLength, 0);
605 (void)usbd_do_request(ucom->sc_udev, &req, NULL);
608 Static device_method_t uftdi_methods[] = {
609 /* Device interface */
610 DEVMETHOD(device_probe, uftdi_match),
611 DEVMETHOD(device_attach, uftdi_attach),
612 DEVMETHOD(device_detach, uftdi_detach),
617 Static driver_t uftdi_driver = {
620 sizeof (struct uftdi_softc)
623 DRIVER_MODULE(uftdi, uhub, uftdi_driver, ucom_devclass, usbd_driver_load, 0);
624 MODULE_DEPEND(uftdi, usb, 1, 1, 1);
625 MODULE_DEPEND(uftdi, ucom,UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);