34 #include <linux/kernel.h>
35 #include <linux/errno.h>
37 #include <linux/slab.h>
38 #include <linux/tty.h>
41 #include <linux/module.h>
46 #include <linux/serial.h>
52 #define DRIVER_DESC "USB FTDI Serial Converters Driver"
106 .probe = ftdi_jtag_probe,
110 .probe = ftdi_mtxorb_hack_setup,
114 .probe = ftdi_NDI_device_setup,
118 .port_probe = ftdi_USB_UIRT_setup,
122 .port_probe = ftdi_HE_TIRA1_setup,
126 .probe = ftdi_stmclite_probe,
130 .probe = ftdi_8u2232c_probe,
885 static const char *ftdi_chip_name[] = {
899 #define FTDI_STATUS_B0_MASK (FTDI_RS0_CTS | FTDI_RS0_DSR | FTDI_RS0_RI | FTDI_RS0_RLSD)
900 #define FTDI_STATUS_B1_MASK (FTDI_RS_BI)
903 #define FTDI_IMPL_ASYNC_FLAGS = (ASYNC_SPD_HI | ASYNC_SPD_VHI \
904 | ASYNC_SPD_CUST | ASYNC_SPD_SHI | ASYNC_SPD_WARP)
914 static void ftdi_process_read_urb(
struct urb *
urb);
917 static void ftdi_set_termios(
struct tty_struct *tty,
919 static int ftdi_tiocmget(
struct tty_struct *tty);
920 static int ftdi_tiocmset(
struct tty_struct *tty,
921 unsigned int set,
unsigned int clear);
922 static int ftdi_get_icount(
struct tty_struct *tty,
925 unsigned int cmd,
unsigned long arg);
926 static void ftdi_break_ctl(
struct tty_struct *tty,
int break_state);
928 static unsigned short int ftdi_232am_baud_base_to_divisor(
int baud,
int base);
929 static unsigned short int ftdi_232am_baud_to_divisor(
int baud);
930 static __u32 ftdi_232bm_baud_base_to_divisor(
int baud,
int base);
931 static __u32 ftdi_232bm_baud_to_divisor(
int baud);
932 static __u32 ftdi_2232h_baud_base_to_divisor(
int baud,
int base);
933 static __u32 ftdi_2232h_baud_to_divisor(
int baud);
940 .description =
"FTDI USB Serial Device",
941 .id_table = id_table_combined,
944 .bulk_out_size = 256,
945 .probe = ftdi_sio_probe,
946 .port_probe = ftdi_sio_port_probe,
947 .port_remove = ftdi_sio_port_remove,
950 .dtr_rts = ftdi_dtr_rts,
953 .process_read_urb = ftdi_process_read_urb,
954 .prepare_write_buffer = ftdi_prepare_write_buffer,
955 .tiocmget = ftdi_tiocmget,
956 .tiocmset = ftdi_tiocmset,
957 .get_icount = ftdi_get_icount,
959 .set_termios = ftdi_set_termios,
960 .break_ctl = ftdi_break_ctl,
964 &ftdi_sio_device,
NULL
968 #define WDR_TIMEOUT 5000
969 #define WDR_SHORT_TIMEOUT 1000
981 static unsigned short int ftdi_232am_baud_base_to_divisor(
int baud,
int base)
985 int divisor3 = base / 2 /
baud;
986 if ((divisor3 & 0x7) == 7)
988 divisor = divisor3 >> 3;
992 else if (divisor3 >= 4)
994 else if (divisor3 != 0)
996 else if (divisor == 1)
1001 static unsigned short int ftdi_232am_baud_to_divisor(
int baud)
1003 return ftdi_232am_baud_base_to_divisor(baud, 48000000);
1006 static __u32 ftdi_232bm_baud_base_to_divisor(
int baud,
int base)
1008 static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 };
1011 int divisor3 = base / 2 /
baud;
1012 divisor = divisor3 >> 3;
1013 divisor |= (
__u32)divfrac[divisor3 & 0x7] << 14;
1017 else if (divisor == 0x4001)
1022 static __u32 ftdi_232bm_baud_to_divisor(
int baud)
1024 return ftdi_232bm_baud_base_to_divisor(baud, 48000000);
1027 static __u32 ftdi_2232h_baud_base_to_divisor(
int baud,
int base)
1029 static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 };
1034 divisor3 = base * 8 / (baud * 10);
1036 divisor = divisor3 >> 3;
1037 divisor |= (
__u32)divfrac[divisor3 & 0x7] << 14;
1041 else if (divisor == 0x4001)
1048 divisor |= 0x00020000;
1052 static __u32 ftdi_2232h_baud_to_divisor(
int baud)
1054 return ftdi_2232h_baud_base_to_divisor(baud, 120000000);
1057 #define set_mctrl(port, set) update_mctrl((port), (set), 0)
1058 #define clear_mctrl(port, clear) update_mctrl((port), 0, (clear))
1069 dev_dbg(dev,
"%s - DTR|RTS not being set|cleared\n", __func__);
1079 if (
set & TIOCM_DTR)
1081 if (
set & TIOCM_RTS)
1084 usb_sndctrlpipe(port->
serial->dev, 0),
1090 dev_dbg(dev,
"%s Error from MODEM_CTRL urb: DTR %s, RTS %s\n",
1092 (
set & TIOCM_DTR) ?
"HIGH" : (clear & TIOCM_DTR) ?
"LOW" :
"unchanged",
1093 (
set & TIOCM_RTS) ?
"HIGH" : (clear & TIOCM_RTS) ?
"LOW" :
"unchanged");
1095 dev_dbg(dev,
"%s - DTR %s, RTS %s\n", __func__,
1096 (
set & TIOCM_DTR) ?
"HIGH" : (clear & TIOCM_DTR) ?
"LOW" :
"unchanged",
1097 (
set & TIOCM_RTS) ?
"HIGH" : (clear & TIOCM_RTS) ?
"LOW" :
"unchanged");
1110 __u32 div_value = 0;
1145 dev_dbg(dev,
"%s - tty_get_baud_rate reports speed %d\n", __func__, baud);
1150 if (baud == 38400 &&
1154 dev_dbg(dev,
"%s - custom divisor %d sets baud rate to %d\n",
1176 if (div_value == 0) {
1177 dev_dbg(dev,
"%s - Baudrate (%d) requested is not supported\n",
1185 if (baud <= 3000000) {
1186 div_value = ftdi_232am_baud_to_divisor(baud);
1188 dev_dbg(dev,
"%s - Baud rate too high!\n", __func__);
1190 div_value = ftdi_232am_baud_to_divisor(9600);
1198 if (baud <= 3000000) {
1200 port->
serial->dev->descriptor.idProduct);
1209 div_value = ftdi_232bm_baud_to_divisor(baud);
1211 dev_dbg(dev,
"%s - Baud rate too high!\n", __func__);
1212 div_value = ftdi_232bm_baud_to_divisor(9600);
1220 if ((baud <= 12000000) && (baud >= 1200)) {
1221 div_value = ftdi_2232h_baud_to_divisor(baud);
1222 }
else if (baud < 1200) {
1223 div_value = ftdi_232bm_baud_to_divisor(baud);
1225 dev_dbg(dev,
"%s - Baud rate too high!\n", __func__);
1226 div_value = ftdi_232bm_baud_to_divisor(9600);
1234 dev_dbg(dev,
"%s - Baud rate set to %d (divisor 0x%lX) on chip %s\n",
1235 __func__, baud, (
unsigned long)div_value,
1245 struct ftdi_private *priv = usb_get_serial_port_data(port);
1248 __u32 urb_index_value;
1251 urb_index_value = get_ftdi_divisor(tty, port);
1252 urb_value = (
__u16)urb_index_value;
1253 urb_index = (
__u16)(urb_index_value >> 16);
1262 usb_sndctrlpipe(port->
serial->dev, 0),
1265 urb_value, urb_index,
1272 struct ftdi_private *priv = usb_get_serial_port_data(port);
1280 dev_dbg(&port->
dev,
"%s: setting latency timer = %i\n", __func__, l);
1283 usb_sndctrlpipe(udev, 0),
1289 dev_err(&port->
dev,
"Unable to write latency timer: %i\n", rv);
1295 struct ftdi_private *priv = usb_get_serial_port_data(port);
1296 struct usb_device *udev = port->
serial->dev;
1305 usb_rcvctrlpipe(udev, 0),
1311 dev_err(&port->
dev,
"Unable to read latency timer: %i\n", rv);
1323 struct ftdi_private *priv = usb_get_serial_port_data(port);
1337 static int set_serial_info(
struct tty_struct *tty,
1340 struct ftdi_private *priv = usb_get_serial_port_data(port);
1361 goto check_and_exit;
1364 if (new_serial.baud_base != priv->
baud_base) {
1375 write_latency_timer(port);
1395 change_speed(tty, port);
1406 struct ftdi_private *priv = usb_get_serial_port_data(port);
1415 if (
copy_to_user(retinfo, &result,
sizeof(
unsigned int)))
1424 struct ftdi_private *priv = usb_get_serial_port_data(port);
1426 struct usb_device *udev = serial->
dev;
1428 unsigned interfaces;
1433 version =
le16_to_cpu(udev->descriptor.bcdDevice);
1434 interfaces = udev->actconfig->desc.bNumInterfaces;
1435 dev_dbg(&port->
dev,
"%s: bcdDevice = 0x%x, bNumInterfaces = %u\n", __func__,
1436 version, interfaces);
1437 if (interfaces > 1) {
1441 if (version == 0x0800) {
1445 }
else if (version == 0x0700) {
1453 inter = serial->
interface->altsetting->desc.bInterfaceNumber;
1456 }
else if (inter == 1) {
1458 }
else if (inter == 2) {
1460 }
else if (inter == 3) {
1465 if (version < 0x500) {
1467 "%s: something fishy - bcdDevice too low for multi-interface device\n",
1470 }
else if (version < 0x200) {
1474 }
else if (version < 0x400) {
1479 }
else if (version < 0x600) {
1482 }
else if (version < 0x900) {
1485 }
else if (version < 0x1000) {
1502 struct ftdi_private *priv = usb_get_serial_port_data(port);
1504 struct usb_device *udev = serial->
dev;
1509 unsigned num_endpoints;
1512 num_endpoints =
interface->cur_altsetting->desc.bNumEndpoints;
1513 dev_info(&udev->dev,
"Number of endpoints %d\n", num_endpoints);
1519 for (i = 0; i < num_endpoints; i++) {
1520 dev_info(&udev->dev,
"Endpoint %d MaxPacketSize %d\n", i+1,
1521 interface->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
1522 ep_desc = &
interface->cur_altsetting->endpoint[
i].desc;
1525 dev_info(&udev->dev,
"Overriding wMaxPacketSize on endpoint %d\n", i);
1546 struct ftdi_private *priv = usb_get_serial_port_data(port);
1560 struct ftdi_private *priv = usb_get_serial_port_data(port);
1565 rv = write_latency_timer(port);
1577 struct ftdi_private *priv = usb_get_serial_port_data(port);
1578 struct usb_device *udev = port->
serial->dev;
1582 dev_dbg(&port->
dev,
"%s: setting event char = %i\n", __func__, v);
1585 usb_sndctrlpipe(udev, 0),
1591 dev_dbg(&port->
dev,
"Unable to write event character: %i\n", rv);
1599 store_latency_timer);
1604 struct ftdi_private *priv = usb_get_serial_port_data(port);
1621 &dev_attr_latency_timer);
1629 struct ftdi_private *priv = usb_get_serial_port_data(port);
1654 static int ftdi_sio_probe(
struct usb_serial *serial,
1666 usb_set_serial_data(serial, (
void *)id->driver_info);
1679 dev_err(&port->
dev,
"%s- kmalloc(%Zd) failed.\n", __func__,
1684 kref_init(&priv->
kref);
1696 usb_set_serial_port_data(port, priv);
1698 ftdi_determine_type(port);
1699 ftdi_set_max_packet_size(port);
1700 if (read_latency_timer(port) < 0)
1702 write_latency_timer(port);
1703 create_sysfs_attrs(port);
1710 static void ftdi_USB_UIRT_setup(
struct ftdi_private *priv)
1720 static void ftdi_HE_TIRA1_setup(
struct ftdi_private *priv)
1733 static int ndi_latency_timer = 1;
1740 static int ftdi_NDI_device_setup(
struct usb_serial *serial)
1742 struct usb_device *udev = serial->
dev;
1743 int latency = ndi_latency_timer;
1750 dev_dbg(&udev->dev,
"%s setting NDI device latency to %d\n", __func__, latency);
1751 dev_info(&udev->dev,
"NDI device with a latency value of %d\n", latency);
1766 static int ftdi_jtag_probe(
struct usb_serial *serial)
1768 struct usb_device *udev = serial->
dev;
1771 if (
interface == udev->actconfig->interface[0]) {
1773 "Ignoring serial port reserved for JTAG\n");
1780 static int ftdi_8u2232c_probe(
struct usb_serial *serial)
1782 struct usb_device *udev = serial->
dev;
1784 if ((udev->manufacturer && !
strcmp(udev->manufacturer,
"CALAO Systems")) ||
1785 (udev->product && !
strcmp(udev->product,
"BeagleBone/XDS100V2")))
1786 return ftdi_jtag_probe(serial);
1795 static int ftdi_stmclite_probe(
struct usb_serial *serial)
1797 struct usb_device *udev = serial->
dev;
1800 if (
interface == udev->actconfig->interface[2])
1803 dev_info(&udev->dev,
"Ignoring serial port reserved for JTAG\n");
1812 static int ftdi_mtxorb_hack_setup(
struct usb_serial *serial)
1814 struct usb_host_endpoint *ep = serial->
dev->ep_in[1];
1820 "Fixing invalid wMaxPacketSize on read pipe\n");
1826 static void ftdi_sio_priv_release(
struct kref *
k)
1835 struct ftdi_private *priv = usb_get_serial_port_data(port);
1840 remove_sysfs_attrs(port);
1842 kref_put(&priv->
kref, ftdi_sio_priv_release);
1850 struct usb_device *dev = port->
serial->dev;
1851 struct ftdi_private *priv = usb_get_serial_port_data(port);
1868 ftdi_set_termios(tty, port, &
dummy);
1874 kref_get(&priv->
kref);
1881 struct ftdi_private *priv = usb_get_serial_port_data(port);
1884 if (!port->
serial->disconnected) {
1887 usb_sndctrlpipe(port->
serial->dev, 0),
1892 dev_err(&port->
dev,
"error from flowcontrol urb\n");
1910 struct ftdi_private *priv = usb_get_serial_port_data(port);
1913 kref_put(&priv->
kref, ftdi_sio_priv_release);
1928 unsigned long flags;
1930 priv = usb_get_serial_port_data(port);
1944 buffer[
i] = (c << 2) + 1;
1947 spin_unlock_irqrestore(&port->
lock, flags);
1957 #define FTDI_RS_ERR_MASK (FTDI_RS_BI | FTDI_RS_PE | FTDI_RS_FE | FTDI_RS_OE)
1959 static int ftdi_process_packet(
struct tty_struct *tty,
2027 if (port->
port.console && port->
sysrq) {
2028 for (i = 0; i < len; i++, ch++) {
2030 tty_insert_flip_char(tty, *ch, flag);
2039 static void ftdi_process_read_urb(
struct urb *
urb)
2043 struct ftdi_private *priv = usb_get_serial_port_data(port);
2044 char *
data = (
char *)urb->transfer_buffer;
2055 count += ftdi_process_packet(tty, port, priv, &data[i], len);
2063 static void ftdi_break_ctl(
struct tty_struct *tty,
int break_state)
2066 struct ftdi_private *priv = usb_get_serial_port_data(port);
2079 usb_sndctrlpipe(port->
serial->dev, 0),
2084 dev_err(&port->
dev,
"%s FAILED to enable/disable break state (state was %d)\n",
2085 __func__, break_state);
2088 dev_dbg(&port->
dev,
"%s break state is %d - urb is %d\n", __func__,
2089 break_state, urb_value);
2097 static void ftdi_set_termios(
struct tty_struct *tty,
2100 struct usb_device *dev = port->
serial->dev;
2102 struct ftdi_private *priv = usb_get_serial_port_data(port);
2108 unsigned int iflag = termios->
c_iflag;
2109 unsigned char vstop;
2115 dev_dbg(ddev,
"%s: forcing baud rate for this device\n", __func__);
2122 dev_dbg(ddev,
"%s: forcing rtscts for this device\n", __func__);
2134 goto no_c_cflag_changes;
2142 goto no_data_parity_stop_changes;
2152 urb_value |= cflag &
PARODD ?
2156 urb_value |= cflag &
PARODD ?
2162 if (cflag &
CSIZE) {
2163 switch (cflag & CSIZE) {
2166 dev_dbg(ddev,
"Setting CS7\n");
2170 dev_dbg(ddev,
"Setting CS8\n");
2173 dev_err(ddev,
"CSIZE was set but not CS7-CS8\n");
2186 dev_err(ddev,
"%s FAILED to set databits/stopbits/parity\n",
2191 no_data_parity_stop_changes:
2199 dev_err(ddev,
"%s error from disable flowcontrol urb\n",
2207 if (change_speed(tty, port))
2208 dev_err(ddev,
"%s urb failed to set baudrate\n", __func__);
2211 if (!old_termios || (old_termios->
c_cflag & CBAUD) ==
B0)
2219 dev_dbg(ddev,
"%s Setting to CRTSCTS flow control\n", __func__);
2221 usb_sndctrlpipe(dev, 0),
2226 dev_err(ddev,
"urb failed to set to rts/cts flow control\n");
2236 if (iflag &
IXOFF) {
2237 dev_dbg(ddev,
"%s request to enable xonxoff iflag=%04x\n",
2247 urb_value = (vstop << 8) | (vstart);
2250 usb_sndctrlpipe(dev, 0),
2257 "xon/xoff flow control\n");
2263 dev_dbg(ddev,
"%s Turning off hardware flow control\n", __func__);
2265 usb_sndctrlpipe(dev, 0),
2270 dev_err(ddev,
"urb failed to clear flow control\n");
2276 static int ftdi_tiocmget(
struct tty_struct *tty)
2279 struct ftdi_private *priv = usb_get_serial_port_data(port);
2311 usb_rcvctrlpipe(port->
serial->dev, 0),
2329 static int ftdi_tiocmset(
struct tty_struct *tty,
2330 unsigned int set,
unsigned int clear)
2334 return update_mctrl(port,
set, clear);
2337 static int ftdi_get_icount(
struct tty_struct *tty,
2341 struct ftdi_private *priv = usb_get_serial_port_data(port);
2348 icount->
tx = ic->
tx;
2349 icount->
rx = ic->
rx;
2358 static int ftdi_ioctl(
struct tty_struct *tty,
2359 unsigned int cmd,
unsigned long arg)
2362 struct ftdi_private *priv = usb_get_serial_port_data(port);
2366 dev_dbg(&port->
dev,
"%s cmd 0x%04x\n", __func__, cmd);
2372 return get_serial_info(port,
2376 return set_serial_info(tty, port,
2395 if (((arg &
TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2396 ((arg &
TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2397 ((arg &
TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2398 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2406 return get_lsr_info(port, (
struct serial_struct __user *)arg);
2414 dev_dbg(&port->
dev,
"%s arg not supported - it was 0x%04x - check /usr/include/asm/ioctls.h\n",
2419 static int __init ftdi_init(
void)
2424 for (i = 0; id_table_combined[
i].
idVendor; i++)
2426 id_table_combined[
i].
match_flags = USB_DEVICE_ID_MATCH_DEVICE;
2433 static void __exit ftdi_exit(
void)