1 /* $NetBSD: uftdi.c,v 1.12 2002/07/18 14:44:10 scw 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 <dev/usb/usb.h>
69 #include <dev/usb/usbhid.h>
71 #include <dev/usb/usbdi.h>
72 #include <dev/usb/usbdi_util.h>
73 #include <dev/usb/usbdevs.h>
75 #include <dev/usb/ucomvar.h>
77 #include <dev/usb/uftdireg.h>
80 #define DPRINTF(x) if (uftdidebug) printf x
81 #define DPRINTFN(n,x) if (uftdidebug>(n)) printf x
88 #define UFTDI_CONFIG_INDEX 0
89 #define UFTDI_IFACE_INDEX 0
93 * These are the maximum number of bytes transferred per frame.
94 * The output buffer size cannot be increased due to the size encoding.
96 #define UFTDIIBUFSIZE 64
97 #define UFTDIOBUFSIZE 64
100 struct ucom_softc sc_ucom;
102 usbd_interface_handle sc_iface; /* interface */
104 enum uftdi_type sc_type;
115 Static void uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr);
116 Static void uftdi_set(void *, int, int, int);
117 Static int uftdi_param(void *, int, struct termios *);
118 Static int uftdi_open(void *sc, int portno);
119 Static void uftdi_read(void *sc, int portno, u_char **ptr,u_int32_t *count);
120 Static void uftdi_write(void *sc, int portno, u_char *to, u_char *from,
122 Static void uftdi_break(void *sc, int portno, int onoff);
124 struct ucom_callback uftdi_callback = {
137 USB_MATCH_START(uftdi, uaa);
139 if (uaa->iface != NULL)
140 return (UMATCH_NONE);
142 DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n",
143 uaa->vendor, uaa->product));
145 if (uaa->vendor == USB_VENDOR_FTDI &&
146 (uaa->product == USB_PRODUCT_FTDI_SERIAL_8U100AX ||
147 uaa->product == USB_PRODUCT_FTDI_SERIAL_8U232AM))
148 return (UMATCH_VENDOR_PRODUCT);
150 return (UMATCH_NONE);
155 USB_ATTACH_START(uftdi, sc, uaa);
156 usbd_device_handle dev = uaa->device;
157 usbd_interface_handle iface;
158 usb_interface_descriptor_t *id;
159 usb_endpoint_descriptor_t *ed;
164 struct ucom_softc *ucom = &sc->sc_ucom;
165 DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
166 devinfo = malloc(1024, M_USBDEV, M_WAITOK);
171 devname = USBDEVNAME(ucom->sc_dev);
173 /* Move the device into the configured state. */
174 err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
176 printf("\n%s: failed to set configuration, err=%s\n",
177 devname, usbd_errstr(err));
181 err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface);
183 printf("\n%s: failed to get interface, err=%s\n",
184 devname, usbd_errstr(err));
188 usbd_devinfo(dev, 0, devinfo);
189 /* USB_ATTACH_SETUP;*/
190 printf("%s: %s\n", devname, devinfo);
192 id = usbd_get_interface_descriptor(iface);
193 ucom->sc_iface = iface;
194 switch( uaa->product ){
195 case USB_PRODUCT_FTDI_SERIAL_8U100AX:
196 sc->sc_type = UFTDI_TYPE_SIO;
199 case USB_PRODUCT_FTDI_SERIAL_8U232AM:
200 sc->sc_type = UFTDI_TYPE_8U232AM;
204 default: /* Can't happen */
208 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
210 for (i = 0; i < id->bNumEndpoints; i++) {
212 ed = usbd_interface2endpoint_descriptor(iface, i);
214 printf("%s: could not read endpoint descriptor"
215 ": %s\n", devname, usbd_errstr(err));
219 addr = ed->bEndpointAddress;
220 dir = UE_GET_DIR(ed->bEndpointAddress);
221 attr = ed->bmAttributes & UE_XFERTYPE;
222 if (dir == UE_DIR_IN && attr == UE_BULK)
223 ucom->sc_bulkin_no = addr;
224 else if (dir == UE_DIR_OUT && attr == UE_BULK)
225 ucom->sc_bulkout_no = addr;
227 printf("%s: unexpected endpoint\n", devname);
231 if (ucom->sc_bulkin_no == -1) {
232 printf("%s: Could not find data bulk in\n",
236 if (ucom->sc_bulkout_no == -1) {
237 printf("%s: Could not find data bulk out\n",
241 ucom->sc_parent = sc;
242 ucom->sc_portno = FTDI_PIT_SIOA;
243 /* bulkin, bulkout set above */
245 ucom->sc_ibufsize = UFTDIIBUFSIZE;
246 ucom->sc_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
247 ucom->sc_ibufsizepad = UFTDIIBUFSIZE;
248 ucom->sc_opkthdrlen = sc->sc_hdrlen;
251 ucom->sc_callback = &uftdi_callback;
253 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
254 USBDEV(ucom->sc_dev));
256 DPRINTF(("uftdi: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
257 ucom_attach(&sc->sc_ucom);
258 free(devinfo, M_USBDEV);
260 USB_ATTACH_SUCCESS_RETURN;
263 DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
265 free(devinfo, M_USBDEV);
267 USB_ATTACH_ERROR_RETURN;
271 uftdi_activate(device_ptr_t self, enum devact act)
273 struct uftdi_softc *sc = (struct uftdi_softc *)self;
281 case DVACT_DEACTIVATE:
282 if (sc->sc_subdev != NULL)
283 rv = config_deactivate(sc->sc_subdev);
293 USB_DETACH_START(uftdi, sc);
297 DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
299 rv = ucom_detach(&sc->sc_ucom);
305 uftdi_open(void *vsc, int portno)
307 struct uftdi_softc *sc = vsc;
308 struct ucom_softc *ucom = (struct ucom_softc *) vsc;
309 usb_device_request_t req;
313 DPRINTF(("uftdi_open: sc=%p\n", sc));
318 /* Perform a full reset on the device */
319 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
320 req.bRequest = FTDI_SIO_RESET;
321 USETW(req.wValue, FTDI_SIO_RESET_SIO);
322 USETW(req.wIndex, portno);
323 USETW(req.wLength, 0);
324 err = usbd_do_request(ucom->sc_udev, &req, NULL);
328 /* Set 9600 baud, 2 stop bits, no parity, 8 bits */
330 t.c_cflag = CSTOPB | CS8;
331 (void)uftdi_param(sc, portno, &t);
333 /* Turn on RTS/CTS flow control */
334 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
335 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
336 USETW(req.wValue, 0);
337 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
338 USETW(req.wLength, 0);
339 err = usbd_do_request(ucom->sc_udev, &req, NULL);
347 uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
349 struct uftdi_softc *sc = vsc;
352 DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
355 msr = FTDI_GET_MSR(*ptr);
356 lsr = FTDI_GET_LSR(*ptr);
360 DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
361 "0x%02x\n", sc, portno, *count, (*ptr)[2]));
364 if (sc->sc_msr != msr ||
365 (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
366 DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
367 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
371 ucom_status_change(&sc->sc_ucom);
374 /* Pick up status and adjust data part. */
380 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
382 struct uftdi_softc *sc = vsc;
384 DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
385 vsc, portno, *count, from[0]));
387 /* Make length tag and copy data */
388 if (sc->sc_hdrlen > 0)
389 *to = FTDI_OUT_TAG(*count, portno);
391 memcpy(to + sc->sc_hdrlen, from, *count);
392 *count += sc->sc_hdrlen;
396 uftdi_set(void *vsc, int portno, int reg, int onoff)
398 struct uftdi_softc *sc = vsc;
399 struct ucom_softc *ucom = vsc;
400 usb_device_request_t req;
403 DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
408 ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
411 ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
414 uftdi_break(sc, portno, onoff);
419 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
420 req.bRequest = FTDI_SIO_MODEM_CTRL;
421 USETW(req.wValue, ctl);
422 USETW(req.wIndex, portno);
423 USETW(req.wLength, 0);
424 DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
425 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
426 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
427 (void)usbd_do_request(ucom->sc_udev, &req, NULL);
431 uftdi_param(void *vsc, int portno, struct termios *t)
433 struct uftdi_softc *sc = vsc;
434 struct ucom_softc *ucom = vsc;
435 usb_device_request_t req;
437 int rate=0, data, flow;
439 DPRINTF(("uftdi_param: sc=%p\n", sc));
444 switch (sc->sc_type) {
446 switch (t->c_ospeed) {
447 case 300: rate = ftdi_sio_b300; break;
448 case 600: rate = ftdi_sio_b600; break;
449 case 1200: rate = ftdi_sio_b1200; break;
450 case 2400: rate = ftdi_sio_b2400; break;
451 case 4800: rate = ftdi_sio_b4800; break;
452 case 9600: rate = ftdi_sio_b9600; break;
453 case 19200: rate = ftdi_sio_b19200; break;
454 case 38400: rate = ftdi_sio_b38400; break;
455 case 57600: rate = ftdi_sio_b57600; break;
456 case 115200: rate = ftdi_sio_b115200; break;
462 case UFTDI_TYPE_8U232AM:
463 switch(t->c_ospeed) {
464 case 300: rate = ftdi_8u232am_b300; break;
465 case 600: rate = ftdi_8u232am_b600; break;
466 case 1200: rate = ftdi_8u232am_b1200; break;
467 case 2400: rate = ftdi_8u232am_b2400; break;
468 case 4800: rate = ftdi_8u232am_b4800; break;
469 case 9600: rate = ftdi_8u232am_b9600; break;
470 case 19200: rate = ftdi_8u232am_b19200; break;
471 case 38400: rate = ftdi_8u232am_b38400; break;
472 case 57600: rate = ftdi_8u232am_b57600; break;
473 case 115200: rate = ftdi_8u232am_b115200; break;
474 case 230400: rate = ftdi_8u232am_b230400; break;
475 case 460800: rate = ftdi_8u232am_b460800; break;
476 case 921600: rate = ftdi_8u232am_b921600; break;
482 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
483 req.bRequest = FTDI_SIO_SET_BAUD_RATE;
484 USETW(req.wValue, rate);
485 USETW(req.wIndex, portno);
486 USETW(req.wLength, 0);
487 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
488 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
489 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
490 err = usbd_do_request(ucom->sc_udev, &req, NULL);
494 if (ISSET(t->c_cflag, CSTOPB))
495 data = FTDI_SIO_SET_DATA_STOP_BITS_2;
497 data = FTDI_SIO_SET_DATA_STOP_BITS_1;
498 if (ISSET(t->c_cflag, PARENB)) {
499 if (ISSET(t->c_cflag, PARODD))
500 data |= FTDI_SIO_SET_DATA_PARITY_ODD;
502 data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
504 data |= FTDI_SIO_SET_DATA_PARITY_NONE;
505 switch (ISSET(t->c_cflag, CSIZE)) {
507 data |= FTDI_SIO_SET_DATA_BITS(5);
510 data |= FTDI_SIO_SET_DATA_BITS(6);
513 data |= FTDI_SIO_SET_DATA_BITS(7);
516 data |= FTDI_SIO_SET_DATA_BITS(8);
521 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
522 req.bRequest = FTDI_SIO_SET_DATA;
523 USETW(req.wValue, data);
524 USETW(req.wIndex, portno);
525 USETW(req.wLength, 0);
526 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
527 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
528 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
529 err = usbd_do_request(ucom->sc_udev, &req, NULL);
533 if (ISSET(t->c_cflag, CRTSCTS)) {
534 flow = FTDI_SIO_RTS_CTS_HS;
535 USETW(req.wValue, 0);
536 } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
537 flow = FTDI_SIO_XON_XOFF_HS;
538 USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
540 flow = FTDI_SIO_DISABLE_FLOW_CTRL;
541 USETW(req.wValue, 0);
543 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
544 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
545 USETW2(req.wIndex, flow, portno);
546 USETW(req.wLength, 0);
547 err = usbd_do_request(ucom->sc_udev, &req, NULL);
555 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
557 struct uftdi_softc *sc = vsc;
559 DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
560 sc->sc_msr, sc->sc_lsr));
569 uftdi_break(void *vsc, int portno, int onoff)
571 struct uftdi_softc *sc = vsc;
572 struct ucom_softc *ucom = vsc;
574 usb_device_request_t req;
577 DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
581 data = sc->last_lcr | FTDI_SIO_SET_BREAK;
586 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
587 req.bRequest = FTDI_SIO_SET_DATA;
588 USETW(req.wValue, data);
589 USETW(req.wIndex, portno);
590 USETW(req.wLength, 0);
591 (void)usbd_do_request(ucom->sc_udev, &req, NULL);
594 Static device_method_t uftdi_methods[] = {
595 /* Device interface */
596 DEVMETHOD(device_probe, uftdi_match),
597 DEVMETHOD(device_attach, uftdi_attach),
598 DEVMETHOD(device_detach, uftdi_detach),
603 Static driver_t uftdi_driver = {
606 sizeof (struct uftdi_softc)
609 DRIVER_MODULE(uftdi, uhub, uftdi_driver, ucom_devclass, usbd_driver_load, 0);
610 MODULE_DEPEND(uftdi, usb, 1, 1, 1);
611 MODULE_DEPEND(uftdi, ucom,UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);