.. | .. |
---|
10 | 10 | * Copyright (C) 2002 |
---|
11 | 11 | * Kuba Ober (kuba@mareimbrium.org) |
---|
12 | 12 | * |
---|
13 | | - * See Documentation/usb/usb-serial.txt for more information on using this |
---|
| 13 | + * See Documentation/usb/usb-serial.rst for more information on using this |
---|
14 | 14 | * driver |
---|
15 | 15 | * |
---|
16 | 16 | * See http://ftdi-usb-sio.sourceforge.net for up to date testing info |
---|
.. | .. |
---|
39 | 39 | #include <linux/uaccess.h> |
---|
40 | 40 | #include <linux/usb.h> |
---|
41 | 41 | #include <linux/serial.h> |
---|
| 42 | +#include <linux/gpio/driver.h> |
---|
42 | 43 | #include <linux/usb/serial.h> |
---|
43 | 44 | #include "ftdi_sio.h" |
---|
44 | 45 | #include "ftdi_sio_ids.h" |
---|
.. | .. |
---|
72 | 73 | unsigned int latency; /* latency setting in use */ |
---|
73 | 74 | unsigned short max_packet_size; |
---|
74 | 75 | struct mutex cfg_lock; /* Avoid mess by parallel calls of config ioctl() and change_speed() */ |
---|
| 76 | +#ifdef CONFIG_GPIOLIB |
---|
| 77 | + struct gpio_chip gc; |
---|
| 78 | + struct mutex gpio_lock; /* protects GPIO state */ |
---|
| 79 | + bool gpio_registered; /* is the gpiochip in kernel registered */ |
---|
| 80 | + bool gpio_used; /* true if the user requested a gpio */ |
---|
| 81 | + u8 gpio_altfunc; /* which pins are in gpio mode */ |
---|
| 82 | + u8 gpio_output; /* pin directions cache */ |
---|
| 83 | + u8 gpio_value; /* pin value for outputs */ |
---|
| 84 | +#endif |
---|
75 | 85 | }; |
---|
76 | 86 | |
---|
77 | 87 | /* struct ftdi_sio_quirk is used by devices requiring special attention. */ |
---|
.. | .. |
---|
1013 | 1023 | { USB_DEVICE(FTDI_VID, CHETCO_SEASMART_DISPLAY_PID) }, |
---|
1014 | 1024 | { USB_DEVICE(FTDI_VID, CHETCO_SEASMART_LITE_PID) }, |
---|
1015 | 1025 | { USB_DEVICE(FTDI_VID, CHETCO_SEASMART_ANALOG_PID) }, |
---|
| 1026 | + /* Belimo Automation devices */ |
---|
| 1027 | + { USB_DEVICE(FTDI_VID, BELIMO_ZTH_PID) }, |
---|
| 1028 | + { USB_DEVICE(FTDI_VID, BELIMO_ZIP_PID) }, |
---|
1016 | 1029 | /* ICP DAS I-756xU devices */ |
---|
1017 | 1030 | { USB_DEVICE(ICPDAS_VID, ICPDAS_I7560U_PID) }, |
---|
1018 | 1031 | { USB_DEVICE(ICPDAS_VID, ICPDAS_I7561U_PID) }, |
---|
.. | .. |
---|
1032 | 1045 | /* IDS GmbH devices */ |
---|
1033 | 1046 | { USB_DEVICE(IDS_VID, IDS_SI31A_PID) }, |
---|
1034 | 1047 | { USB_DEVICE(IDS_VID, IDS_CM31A_PID) }, |
---|
| 1048 | + /* Omron devices */ |
---|
| 1049 | + { USB_DEVICE(OMRON_VID, OMRON_CS1W_CIF31_PID) }, |
---|
1035 | 1050 | /* U-Blox devices */ |
---|
1036 | 1051 | { USB_DEVICE(UBLOX_VID, UBLOX_C099F9P_ZED_PID) }, |
---|
1037 | 1052 | { USB_DEVICE(UBLOX_VID, UBLOX_C099F9P_ODIN_PID) }, |
---|
.. | .. |
---|
1080 | 1095 | unsigned int set, unsigned int clear); |
---|
1081 | 1096 | static int ftdi_ioctl(struct tty_struct *tty, |
---|
1082 | 1097 | unsigned int cmd, unsigned long arg); |
---|
| 1098 | +static int get_serial_info(struct tty_struct *tty, |
---|
| 1099 | + struct serial_struct *ss); |
---|
| 1100 | +static int set_serial_info(struct tty_struct *tty, |
---|
| 1101 | + struct serial_struct *ss); |
---|
1083 | 1102 | static void ftdi_break_ctl(struct tty_struct *tty, int break_state); |
---|
1084 | 1103 | static bool ftdi_tx_empty(struct usb_serial_port *port); |
---|
1085 | 1104 | static int ftdi_get_modem_status(struct usb_serial_port *port, |
---|
.. | .. |
---|
1116 | 1135 | .tiocmiwait = usb_serial_generic_tiocmiwait, |
---|
1117 | 1136 | .get_icount = usb_serial_generic_get_icount, |
---|
1118 | 1137 | .ioctl = ftdi_ioctl, |
---|
| 1138 | + .get_serial = get_serial_info, |
---|
| 1139 | + .set_serial = set_serial_info, |
---|
1119 | 1140 | .set_termios = ftdi_set_termios, |
---|
1120 | 1141 | .break_ctl = ftdi_break_ctl, |
---|
1121 | 1142 | .tx_empty = ftdi_tx_empty, |
---|
.. | .. |
---|
1139 | 1160 | { |
---|
1140 | 1161 | unsigned short int divisor; |
---|
1141 | 1162 | /* divisor shifted 3 bits to the left */ |
---|
1142 | | - int divisor3 = base / 2 / baud; |
---|
| 1163 | + int divisor3 = DIV_ROUND_CLOSEST(base, 2 * baud); |
---|
1143 | 1164 | if ((divisor3 & 0x7) == 7) |
---|
1144 | 1165 | divisor3++; /* round x.7/8 up to x+1 */ |
---|
1145 | 1166 | divisor = divisor3 >> 3; |
---|
.. | .. |
---|
1165 | 1186 | static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 }; |
---|
1166 | 1187 | u32 divisor; |
---|
1167 | 1188 | /* divisor shifted 3 bits to the left */ |
---|
1168 | | - int divisor3 = base / 2 / baud; |
---|
| 1189 | + int divisor3 = DIV_ROUND_CLOSEST(base, 2 * baud); |
---|
1169 | 1190 | divisor = divisor3 >> 3; |
---|
1170 | 1191 | divisor |= (u32)divfrac[divisor3 & 0x7] << 14; |
---|
1171 | 1192 | /* Deal with special cases for highest baud rates. */ |
---|
.. | .. |
---|
1188 | 1209 | int divisor3; |
---|
1189 | 1210 | |
---|
1190 | 1211 | /* hi-speed baud rate is 10-bit sampling instead of 16-bit */ |
---|
1191 | | - divisor3 = base * 8 / (baud * 10); |
---|
| 1212 | + divisor3 = DIV_ROUND_CLOSEST(8 * base, 10 * baud); |
---|
1192 | 1213 | |
---|
1193 | 1214 | divisor = divisor3 >> 3; |
---|
1194 | 1215 | divisor |= (u32)divfrac[divisor3 & 0x7] << 14; |
---|
.. | .. |
---|
1299 | 1320 | case 38400: div_value = ftdi_sio_b38400; break; |
---|
1300 | 1321 | case 57600: div_value = ftdi_sio_b57600; break; |
---|
1301 | 1322 | case 115200: div_value = ftdi_sio_b115200; break; |
---|
1302 | | - } /* baud */ |
---|
1303 | | - if (div_value == 0) { |
---|
| 1323 | + default: |
---|
1304 | 1324 | dev_dbg(dev, "%s - Baudrate (%d) requested is not supported\n", |
---|
1305 | 1325 | __func__, baud); |
---|
1306 | 1326 | div_value = ftdi_sio_b9600; |
---|
.. | .. |
---|
1469 | 1489 | return 0; |
---|
1470 | 1490 | } |
---|
1471 | 1491 | |
---|
1472 | | -static int get_serial_info(struct usb_serial_port *port, |
---|
1473 | | - struct serial_struct __user *retinfo) |
---|
| 1492 | +static int get_serial_info(struct tty_struct *tty, |
---|
| 1493 | + struct serial_struct *ss) |
---|
1474 | 1494 | { |
---|
| 1495 | + struct usb_serial_port *port = tty->driver_data; |
---|
1475 | 1496 | struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
1476 | | - struct serial_struct tmp; |
---|
1477 | 1497 | |
---|
1478 | | - memset(&tmp, 0, sizeof(tmp)); |
---|
1479 | | - tmp.flags = priv->flags; |
---|
1480 | | - tmp.baud_base = priv->baud_base; |
---|
1481 | | - tmp.custom_divisor = priv->custom_divisor; |
---|
1482 | | - if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) |
---|
1483 | | - return -EFAULT; |
---|
| 1498 | + ss->flags = priv->flags; |
---|
| 1499 | + ss->baud_base = priv->baud_base; |
---|
| 1500 | + ss->custom_divisor = priv->custom_divisor; |
---|
1484 | 1501 | return 0; |
---|
1485 | 1502 | } |
---|
1486 | 1503 | |
---|
1487 | 1504 | static int set_serial_info(struct tty_struct *tty, |
---|
1488 | | - struct usb_serial_port *port, struct serial_struct __user *newinfo) |
---|
| 1505 | + struct serial_struct *ss) |
---|
1489 | 1506 | { |
---|
| 1507 | + struct usb_serial_port *port = tty->driver_data; |
---|
1490 | 1508 | struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
1491 | | - struct serial_struct new_serial; |
---|
1492 | 1509 | struct ftdi_private old_priv; |
---|
1493 | | - |
---|
1494 | | - if (copy_from_user(&new_serial, newinfo, sizeof(new_serial))) |
---|
1495 | | - return -EFAULT; |
---|
1496 | 1510 | |
---|
1497 | 1511 | mutex_lock(&priv->cfg_lock); |
---|
1498 | 1512 | old_priv = *priv; |
---|
.. | .. |
---|
1500 | 1514 | /* Do error checking and permission checking */ |
---|
1501 | 1515 | |
---|
1502 | 1516 | if (!capable(CAP_SYS_ADMIN)) { |
---|
1503 | | - if ((new_serial.flags ^ priv->flags) & ~ASYNC_USR_MASK) { |
---|
| 1517 | + if ((ss->flags ^ priv->flags) & ~ASYNC_USR_MASK) { |
---|
1504 | 1518 | mutex_unlock(&priv->cfg_lock); |
---|
1505 | 1519 | return -EPERM; |
---|
1506 | 1520 | } |
---|
1507 | 1521 | priv->flags = ((priv->flags & ~ASYNC_USR_MASK) | |
---|
1508 | | - (new_serial.flags & ASYNC_USR_MASK)); |
---|
1509 | | - priv->custom_divisor = new_serial.custom_divisor; |
---|
| 1522 | + (ss->flags & ASYNC_USR_MASK)); |
---|
| 1523 | + priv->custom_divisor = ss->custom_divisor; |
---|
1510 | 1524 | goto check_and_exit; |
---|
1511 | 1525 | } |
---|
1512 | 1526 | |
---|
1513 | | - if (new_serial.baud_base != priv->baud_base) { |
---|
| 1527 | + if (ss->baud_base != priv->baud_base) { |
---|
1514 | 1528 | mutex_unlock(&priv->cfg_lock); |
---|
1515 | 1529 | return -EINVAL; |
---|
1516 | 1530 | } |
---|
.. | .. |
---|
1518 | 1532 | /* Make the changes - these are privileged changes! */ |
---|
1519 | 1533 | |
---|
1520 | 1534 | priv->flags = ((priv->flags & ~ASYNC_FLAGS) | |
---|
1521 | | - (new_serial.flags & ASYNC_FLAGS)); |
---|
1522 | | - priv->custom_divisor = new_serial.custom_divisor; |
---|
| 1535 | + (ss->flags & ASYNC_FLAGS)); |
---|
| 1536 | + priv->custom_divisor = ss->custom_divisor; |
---|
1523 | 1537 | |
---|
1524 | 1538 | check_and_exit: |
---|
1525 | 1539 | write_latency_timer(port); |
---|
.. | .. |
---|
1533 | 1547 | dev_warn_ratelimited(&port->dev, "use of SPD flags is deprecated\n"); |
---|
1534 | 1548 | |
---|
1535 | 1549 | change_speed(tty, port); |
---|
1536 | | - mutex_unlock(&priv->cfg_lock); |
---|
1537 | 1550 | } |
---|
1538 | | - else |
---|
1539 | | - mutex_unlock(&priv->cfg_lock); |
---|
| 1551 | + mutex_unlock(&priv->cfg_lock); |
---|
1540 | 1552 | return 0; |
---|
1541 | 1553 | } |
---|
1542 | 1554 | |
---|
.. | .. |
---|
1572 | 1584 | dev_dbg(&port->dev, "%s: bcdDevice = 0x%x, bNumInterfaces = %u\n", __func__, |
---|
1573 | 1585 | version, interfaces); |
---|
1574 | 1586 | if (interfaces > 1) { |
---|
1575 | | - int inter; |
---|
| 1587 | + struct usb_interface *intf = serial->interface; |
---|
| 1588 | + int ifnum = intf->cur_altsetting->desc.bInterfaceNumber; |
---|
1576 | 1589 | |
---|
1577 | 1590 | /* Multiple interfaces.*/ |
---|
1578 | 1591 | if (version == 0x0800) { |
---|
.. | .. |
---|
1587 | 1600 | priv->chip_type = FT2232C; |
---|
1588 | 1601 | |
---|
1589 | 1602 | /* Determine interface code. */ |
---|
1590 | | - inter = serial->interface->altsetting->desc.bInterfaceNumber; |
---|
1591 | | - if (inter == 0) { |
---|
| 1603 | + if (ifnum == 0) |
---|
1592 | 1604 | priv->interface = INTERFACE_A; |
---|
1593 | | - } else if (inter == 1) { |
---|
| 1605 | + else if (ifnum == 1) |
---|
1594 | 1606 | priv->interface = INTERFACE_B; |
---|
1595 | | - } else if (inter == 2) { |
---|
| 1607 | + else if (ifnum == 2) |
---|
1596 | 1608 | priv->interface = INTERFACE_C; |
---|
1597 | | - } else if (inter == 3) { |
---|
| 1609 | + else if (ifnum == 3) |
---|
1598 | 1610 | priv->interface = INTERFACE_D; |
---|
1599 | | - } |
---|
| 1611 | + |
---|
1600 | 1612 | /* BM-type devices have a bug where bcdDevice gets set |
---|
1601 | 1613 | * to 0x200 when iSerialNumber is 0. */ |
---|
1602 | 1614 | if (version < 0x500) { |
---|
.. | .. |
---|
1792 | 1804 | |
---|
1793 | 1805 | } |
---|
1794 | 1806 | |
---|
| 1807 | +#ifdef CONFIG_GPIOLIB |
---|
| 1808 | + |
---|
| 1809 | +static int ftdi_set_bitmode(struct usb_serial_port *port, u8 mode) |
---|
| 1810 | +{ |
---|
| 1811 | + struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 1812 | + struct usb_serial *serial = port->serial; |
---|
| 1813 | + int result; |
---|
| 1814 | + u16 val; |
---|
| 1815 | + |
---|
| 1816 | + result = usb_autopm_get_interface(serial->interface); |
---|
| 1817 | + if (result) |
---|
| 1818 | + return result; |
---|
| 1819 | + |
---|
| 1820 | + val = (mode << 8) | (priv->gpio_output << 4) | priv->gpio_value; |
---|
| 1821 | + result = usb_control_msg(serial->dev, |
---|
| 1822 | + usb_sndctrlpipe(serial->dev, 0), |
---|
| 1823 | + FTDI_SIO_SET_BITMODE_REQUEST, |
---|
| 1824 | + FTDI_SIO_SET_BITMODE_REQUEST_TYPE, val, |
---|
| 1825 | + priv->interface, NULL, 0, WDR_TIMEOUT); |
---|
| 1826 | + if (result < 0) { |
---|
| 1827 | + dev_err(&serial->interface->dev, |
---|
| 1828 | + "bitmode request failed for value 0x%04x: %d\n", |
---|
| 1829 | + val, result); |
---|
| 1830 | + } |
---|
| 1831 | + |
---|
| 1832 | + usb_autopm_put_interface(serial->interface); |
---|
| 1833 | + |
---|
| 1834 | + return result; |
---|
| 1835 | +} |
---|
| 1836 | + |
---|
| 1837 | +static int ftdi_set_cbus_pins(struct usb_serial_port *port) |
---|
| 1838 | +{ |
---|
| 1839 | + return ftdi_set_bitmode(port, FTDI_SIO_BITMODE_CBUS); |
---|
| 1840 | +} |
---|
| 1841 | + |
---|
| 1842 | +static int ftdi_exit_cbus_mode(struct usb_serial_port *port) |
---|
| 1843 | +{ |
---|
| 1844 | + struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 1845 | + |
---|
| 1846 | + priv->gpio_output = 0; |
---|
| 1847 | + priv->gpio_value = 0; |
---|
| 1848 | + return ftdi_set_bitmode(port, FTDI_SIO_BITMODE_RESET); |
---|
| 1849 | +} |
---|
| 1850 | + |
---|
| 1851 | +static int ftdi_gpio_request(struct gpio_chip *gc, unsigned int offset) |
---|
| 1852 | +{ |
---|
| 1853 | + struct usb_serial_port *port = gpiochip_get_data(gc); |
---|
| 1854 | + struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 1855 | + int result; |
---|
| 1856 | + |
---|
| 1857 | + if (priv->gpio_altfunc & BIT(offset)) |
---|
| 1858 | + return -ENODEV; |
---|
| 1859 | + |
---|
| 1860 | + mutex_lock(&priv->gpio_lock); |
---|
| 1861 | + if (!priv->gpio_used) { |
---|
| 1862 | + /* Set default pin states, as we cannot get them from device */ |
---|
| 1863 | + priv->gpio_output = 0x00; |
---|
| 1864 | + priv->gpio_value = 0x00; |
---|
| 1865 | + result = ftdi_set_cbus_pins(port); |
---|
| 1866 | + if (result) { |
---|
| 1867 | + mutex_unlock(&priv->gpio_lock); |
---|
| 1868 | + return result; |
---|
| 1869 | + } |
---|
| 1870 | + |
---|
| 1871 | + priv->gpio_used = true; |
---|
| 1872 | + } |
---|
| 1873 | + mutex_unlock(&priv->gpio_lock); |
---|
| 1874 | + |
---|
| 1875 | + return 0; |
---|
| 1876 | +} |
---|
| 1877 | + |
---|
| 1878 | +static int ftdi_read_cbus_pins(struct usb_serial_port *port) |
---|
| 1879 | +{ |
---|
| 1880 | + struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 1881 | + struct usb_serial *serial = port->serial; |
---|
| 1882 | + unsigned char *buf; |
---|
| 1883 | + int result; |
---|
| 1884 | + |
---|
| 1885 | + result = usb_autopm_get_interface(serial->interface); |
---|
| 1886 | + if (result) |
---|
| 1887 | + return result; |
---|
| 1888 | + |
---|
| 1889 | + buf = kmalloc(1, GFP_KERNEL); |
---|
| 1890 | + if (!buf) { |
---|
| 1891 | + usb_autopm_put_interface(serial->interface); |
---|
| 1892 | + return -ENOMEM; |
---|
| 1893 | + } |
---|
| 1894 | + |
---|
| 1895 | + result = usb_control_msg(serial->dev, |
---|
| 1896 | + usb_rcvctrlpipe(serial->dev, 0), |
---|
| 1897 | + FTDI_SIO_READ_PINS_REQUEST, |
---|
| 1898 | + FTDI_SIO_READ_PINS_REQUEST_TYPE, 0, |
---|
| 1899 | + priv->interface, buf, 1, WDR_TIMEOUT); |
---|
| 1900 | + if (result < 1) { |
---|
| 1901 | + if (result >= 0) |
---|
| 1902 | + result = -EIO; |
---|
| 1903 | + } else { |
---|
| 1904 | + result = buf[0]; |
---|
| 1905 | + } |
---|
| 1906 | + |
---|
| 1907 | + kfree(buf); |
---|
| 1908 | + usb_autopm_put_interface(serial->interface); |
---|
| 1909 | + |
---|
| 1910 | + return result; |
---|
| 1911 | +} |
---|
| 1912 | + |
---|
| 1913 | +static int ftdi_gpio_get(struct gpio_chip *gc, unsigned int gpio) |
---|
| 1914 | +{ |
---|
| 1915 | + struct usb_serial_port *port = gpiochip_get_data(gc); |
---|
| 1916 | + int result; |
---|
| 1917 | + |
---|
| 1918 | + result = ftdi_read_cbus_pins(port); |
---|
| 1919 | + if (result < 0) |
---|
| 1920 | + return result; |
---|
| 1921 | + |
---|
| 1922 | + return !!(result & BIT(gpio)); |
---|
| 1923 | +} |
---|
| 1924 | + |
---|
| 1925 | +static void ftdi_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value) |
---|
| 1926 | +{ |
---|
| 1927 | + struct usb_serial_port *port = gpiochip_get_data(gc); |
---|
| 1928 | + struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 1929 | + |
---|
| 1930 | + mutex_lock(&priv->gpio_lock); |
---|
| 1931 | + |
---|
| 1932 | + if (value) |
---|
| 1933 | + priv->gpio_value |= BIT(gpio); |
---|
| 1934 | + else |
---|
| 1935 | + priv->gpio_value &= ~BIT(gpio); |
---|
| 1936 | + |
---|
| 1937 | + ftdi_set_cbus_pins(port); |
---|
| 1938 | + |
---|
| 1939 | + mutex_unlock(&priv->gpio_lock); |
---|
| 1940 | +} |
---|
| 1941 | + |
---|
| 1942 | +static int ftdi_gpio_get_multiple(struct gpio_chip *gc, unsigned long *mask, |
---|
| 1943 | + unsigned long *bits) |
---|
| 1944 | +{ |
---|
| 1945 | + struct usb_serial_port *port = gpiochip_get_data(gc); |
---|
| 1946 | + int result; |
---|
| 1947 | + |
---|
| 1948 | + result = ftdi_read_cbus_pins(port); |
---|
| 1949 | + if (result < 0) |
---|
| 1950 | + return result; |
---|
| 1951 | + |
---|
| 1952 | + *bits = result & *mask; |
---|
| 1953 | + |
---|
| 1954 | + return 0; |
---|
| 1955 | +} |
---|
| 1956 | + |
---|
| 1957 | +static void ftdi_gpio_set_multiple(struct gpio_chip *gc, unsigned long *mask, |
---|
| 1958 | + unsigned long *bits) |
---|
| 1959 | +{ |
---|
| 1960 | + struct usb_serial_port *port = gpiochip_get_data(gc); |
---|
| 1961 | + struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 1962 | + |
---|
| 1963 | + mutex_lock(&priv->gpio_lock); |
---|
| 1964 | + |
---|
| 1965 | + priv->gpio_value &= ~(*mask); |
---|
| 1966 | + priv->gpio_value |= *bits & *mask; |
---|
| 1967 | + ftdi_set_cbus_pins(port); |
---|
| 1968 | + |
---|
| 1969 | + mutex_unlock(&priv->gpio_lock); |
---|
| 1970 | +} |
---|
| 1971 | + |
---|
| 1972 | +static int ftdi_gpio_direction_get(struct gpio_chip *gc, unsigned int gpio) |
---|
| 1973 | +{ |
---|
| 1974 | + struct usb_serial_port *port = gpiochip_get_data(gc); |
---|
| 1975 | + struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 1976 | + |
---|
| 1977 | + return !(priv->gpio_output & BIT(gpio)); |
---|
| 1978 | +} |
---|
| 1979 | + |
---|
| 1980 | +static int ftdi_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio) |
---|
| 1981 | +{ |
---|
| 1982 | + struct usb_serial_port *port = gpiochip_get_data(gc); |
---|
| 1983 | + struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 1984 | + int result; |
---|
| 1985 | + |
---|
| 1986 | + mutex_lock(&priv->gpio_lock); |
---|
| 1987 | + |
---|
| 1988 | + priv->gpio_output &= ~BIT(gpio); |
---|
| 1989 | + result = ftdi_set_cbus_pins(port); |
---|
| 1990 | + |
---|
| 1991 | + mutex_unlock(&priv->gpio_lock); |
---|
| 1992 | + |
---|
| 1993 | + return result; |
---|
| 1994 | +} |
---|
| 1995 | + |
---|
| 1996 | +static int ftdi_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio, |
---|
| 1997 | + int value) |
---|
| 1998 | +{ |
---|
| 1999 | + struct usb_serial_port *port = gpiochip_get_data(gc); |
---|
| 2000 | + struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 2001 | + int result; |
---|
| 2002 | + |
---|
| 2003 | + mutex_lock(&priv->gpio_lock); |
---|
| 2004 | + |
---|
| 2005 | + priv->gpio_output |= BIT(gpio); |
---|
| 2006 | + if (value) |
---|
| 2007 | + priv->gpio_value |= BIT(gpio); |
---|
| 2008 | + else |
---|
| 2009 | + priv->gpio_value &= ~BIT(gpio); |
---|
| 2010 | + |
---|
| 2011 | + result = ftdi_set_cbus_pins(port); |
---|
| 2012 | + |
---|
| 2013 | + mutex_unlock(&priv->gpio_lock); |
---|
| 2014 | + |
---|
| 2015 | + return result; |
---|
| 2016 | +} |
---|
| 2017 | + |
---|
| 2018 | +static int ftdi_read_eeprom(struct usb_serial *serial, void *dst, u16 addr, |
---|
| 2019 | + u16 nbytes) |
---|
| 2020 | +{ |
---|
| 2021 | + int read = 0; |
---|
| 2022 | + |
---|
| 2023 | + if (addr % 2 != 0) |
---|
| 2024 | + return -EINVAL; |
---|
| 2025 | + if (nbytes % 2 != 0) |
---|
| 2026 | + return -EINVAL; |
---|
| 2027 | + |
---|
| 2028 | + /* Read EEPROM two bytes at a time */ |
---|
| 2029 | + while (read < nbytes) { |
---|
| 2030 | + int rv; |
---|
| 2031 | + |
---|
| 2032 | + rv = usb_control_msg(serial->dev, |
---|
| 2033 | + usb_rcvctrlpipe(serial->dev, 0), |
---|
| 2034 | + FTDI_SIO_READ_EEPROM_REQUEST, |
---|
| 2035 | + FTDI_SIO_READ_EEPROM_REQUEST_TYPE, |
---|
| 2036 | + 0, (addr + read) / 2, dst + read, 2, |
---|
| 2037 | + WDR_TIMEOUT); |
---|
| 2038 | + if (rv < 2) { |
---|
| 2039 | + if (rv >= 0) |
---|
| 2040 | + return -EIO; |
---|
| 2041 | + else |
---|
| 2042 | + return rv; |
---|
| 2043 | + } |
---|
| 2044 | + |
---|
| 2045 | + read += rv; |
---|
| 2046 | + } |
---|
| 2047 | + |
---|
| 2048 | + return 0; |
---|
| 2049 | +} |
---|
| 2050 | + |
---|
| 2051 | +static int ftdi_gpio_init_ft232h(struct usb_serial_port *port) |
---|
| 2052 | +{ |
---|
| 2053 | + struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 2054 | + u16 cbus_config; |
---|
| 2055 | + u8 *buf; |
---|
| 2056 | + int ret; |
---|
| 2057 | + int i; |
---|
| 2058 | + |
---|
| 2059 | + buf = kmalloc(4, GFP_KERNEL); |
---|
| 2060 | + if (!buf) |
---|
| 2061 | + return -ENOMEM; |
---|
| 2062 | + |
---|
| 2063 | + ret = ftdi_read_eeprom(port->serial, buf, 0x1a, 4); |
---|
| 2064 | + if (ret < 0) |
---|
| 2065 | + goto out_free; |
---|
| 2066 | + |
---|
| 2067 | + /* |
---|
| 2068 | + * FT232H CBUS Memory Map |
---|
| 2069 | + * |
---|
| 2070 | + * 0x1a: X- (upper nibble -> AC5) |
---|
| 2071 | + * 0x1b: -X (lower nibble -> AC6) |
---|
| 2072 | + * 0x1c: XX (upper nibble -> AC9 | lower nibble -> AC8) |
---|
| 2073 | + */ |
---|
| 2074 | + cbus_config = buf[2] << 8 | (buf[1] & 0xf) << 4 | (buf[0] & 0xf0) >> 4; |
---|
| 2075 | + |
---|
| 2076 | + priv->gc.ngpio = 4; |
---|
| 2077 | + priv->gpio_altfunc = 0xff; |
---|
| 2078 | + |
---|
| 2079 | + for (i = 0; i < priv->gc.ngpio; ++i) { |
---|
| 2080 | + if ((cbus_config & 0xf) == FTDI_FTX_CBUS_MUX_GPIO) |
---|
| 2081 | + priv->gpio_altfunc &= ~BIT(i); |
---|
| 2082 | + cbus_config >>= 4; |
---|
| 2083 | + } |
---|
| 2084 | + |
---|
| 2085 | +out_free: |
---|
| 2086 | + kfree(buf); |
---|
| 2087 | + |
---|
| 2088 | + return ret; |
---|
| 2089 | +} |
---|
| 2090 | + |
---|
| 2091 | +static int ftdi_gpio_init_ft232r(struct usb_serial_port *port) |
---|
| 2092 | +{ |
---|
| 2093 | + struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 2094 | + u16 cbus_config; |
---|
| 2095 | + u8 *buf; |
---|
| 2096 | + int ret; |
---|
| 2097 | + int i; |
---|
| 2098 | + |
---|
| 2099 | + buf = kmalloc(2, GFP_KERNEL); |
---|
| 2100 | + if (!buf) |
---|
| 2101 | + return -ENOMEM; |
---|
| 2102 | + |
---|
| 2103 | + ret = ftdi_read_eeprom(port->serial, buf, 0x14, 2); |
---|
| 2104 | + if (ret < 0) |
---|
| 2105 | + goto out_free; |
---|
| 2106 | + |
---|
| 2107 | + cbus_config = le16_to_cpup((__le16 *)buf); |
---|
| 2108 | + dev_dbg(&port->dev, "cbus_config = 0x%04x\n", cbus_config); |
---|
| 2109 | + |
---|
| 2110 | + priv->gc.ngpio = 4; |
---|
| 2111 | + |
---|
| 2112 | + priv->gpio_altfunc = 0xff; |
---|
| 2113 | + for (i = 0; i < priv->gc.ngpio; ++i) { |
---|
| 2114 | + if ((cbus_config & 0xf) == FTDI_FT232R_CBUS_MUX_GPIO) |
---|
| 2115 | + priv->gpio_altfunc &= ~BIT(i); |
---|
| 2116 | + cbus_config >>= 4; |
---|
| 2117 | + } |
---|
| 2118 | +out_free: |
---|
| 2119 | + kfree(buf); |
---|
| 2120 | + |
---|
| 2121 | + return ret; |
---|
| 2122 | +} |
---|
| 2123 | + |
---|
| 2124 | +static int ftdi_gpio_init_ftx(struct usb_serial_port *port) |
---|
| 2125 | +{ |
---|
| 2126 | + struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 2127 | + struct usb_serial *serial = port->serial; |
---|
| 2128 | + const u16 cbus_cfg_addr = 0x1a; |
---|
| 2129 | + const u16 cbus_cfg_size = 4; |
---|
| 2130 | + u8 *cbus_cfg_buf; |
---|
| 2131 | + int result; |
---|
| 2132 | + u8 i; |
---|
| 2133 | + |
---|
| 2134 | + cbus_cfg_buf = kmalloc(cbus_cfg_size, GFP_KERNEL); |
---|
| 2135 | + if (!cbus_cfg_buf) |
---|
| 2136 | + return -ENOMEM; |
---|
| 2137 | + |
---|
| 2138 | + result = ftdi_read_eeprom(serial, cbus_cfg_buf, |
---|
| 2139 | + cbus_cfg_addr, cbus_cfg_size); |
---|
| 2140 | + if (result < 0) |
---|
| 2141 | + goto out_free; |
---|
| 2142 | + |
---|
| 2143 | + /* FIXME: FT234XD alone has 1 GPIO, but how to recognize this IC? */ |
---|
| 2144 | + priv->gc.ngpio = 4; |
---|
| 2145 | + |
---|
| 2146 | + /* Determine which pins are configured for CBUS bitbanging */ |
---|
| 2147 | + priv->gpio_altfunc = 0xff; |
---|
| 2148 | + for (i = 0; i < priv->gc.ngpio; ++i) { |
---|
| 2149 | + if (cbus_cfg_buf[i] == FTDI_FTX_CBUS_MUX_GPIO) |
---|
| 2150 | + priv->gpio_altfunc &= ~BIT(i); |
---|
| 2151 | + } |
---|
| 2152 | + |
---|
| 2153 | +out_free: |
---|
| 2154 | + kfree(cbus_cfg_buf); |
---|
| 2155 | + |
---|
| 2156 | + return result; |
---|
| 2157 | +} |
---|
| 2158 | + |
---|
| 2159 | +static int ftdi_gpio_init(struct usb_serial_port *port) |
---|
| 2160 | +{ |
---|
| 2161 | + struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 2162 | + struct usb_serial *serial = port->serial; |
---|
| 2163 | + int result; |
---|
| 2164 | + |
---|
| 2165 | + switch (priv->chip_type) { |
---|
| 2166 | + case FT232H: |
---|
| 2167 | + result = ftdi_gpio_init_ft232h(port); |
---|
| 2168 | + break; |
---|
| 2169 | + case FT232RL: |
---|
| 2170 | + result = ftdi_gpio_init_ft232r(port); |
---|
| 2171 | + break; |
---|
| 2172 | + case FTX: |
---|
| 2173 | + result = ftdi_gpio_init_ftx(port); |
---|
| 2174 | + break; |
---|
| 2175 | + default: |
---|
| 2176 | + return 0; |
---|
| 2177 | + } |
---|
| 2178 | + |
---|
| 2179 | + if (result < 0) |
---|
| 2180 | + return result; |
---|
| 2181 | + |
---|
| 2182 | + mutex_init(&priv->gpio_lock); |
---|
| 2183 | + |
---|
| 2184 | + priv->gc.label = "ftdi-cbus"; |
---|
| 2185 | + priv->gc.request = ftdi_gpio_request; |
---|
| 2186 | + priv->gc.get_direction = ftdi_gpio_direction_get; |
---|
| 2187 | + priv->gc.direction_input = ftdi_gpio_direction_input; |
---|
| 2188 | + priv->gc.direction_output = ftdi_gpio_direction_output; |
---|
| 2189 | + priv->gc.get = ftdi_gpio_get; |
---|
| 2190 | + priv->gc.set = ftdi_gpio_set; |
---|
| 2191 | + priv->gc.get_multiple = ftdi_gpio_get_multiple; |
---|
| 2192 | + priv->gc.set_multiple = ftdi_gpio_set_multiple; |
---|
| 2193 | + priv->gc.owner = THIS_MODULE; |
---|
| 2194 | + priv->gc.parent = &serial->interface->dev; |
---|
| 2195 | + priv->gc.base = -1; |
---|
| 2196 | + priv->gc.can_sleep = true; |
---|
| 2197 | + |
---|
| 2198 | + result = gpiochip_add_data(&priv->gc, port); |
---|
| 2199 | + if (!result) |
---|
| 2200 | + priv->gpio_registered = true; |
---|
| 2201 | + |
---|
| 2202 | + return result; |
---|
| 2203 | +} |
---|
| 2204 | + |
---|
| 2205 | +static void ftdi_gpio_remove(struct usb_serial_port *port) |
---|
| 2206 | +{ |
---|
| 2207 | + struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 2208 | + |
---|
| 2209 | + if (priv->gpio_registered) { |
---|
| 2210 | + gpiochip_remove(&priv->gc); |
---|
| 2211 | + priv->gpio_registered = false; |
---|
| 2212 | + } |
---|
| 2213 | + |
---|
| 2214 | + if (priv->gpio_used) { |
---|
| 2215 | + /* Exiting CBUS-mode does not reset pin states. */ |
---|
| 2216 | + ftdi_exit_cbus_mode(port); |
---|
| 2217 | + priv->gpio_used = false; |
---|
| 2218 | + } |
---|
| 2219 | +} |
---|
| 2220 | + |
---|
| 2221 | +#else |
---|
| 2222 | + |
---|
| 2223 | +static int ftdi_gpio_init(struct usb_serial_port *port) |
---|
| 2224 | +{ |
---|
| 2225 | + return 0; |
---|
| 2226 | +} |
---|
| 2227 | + |
---|
| 2228 | +static void ftdi_gpio_remove(struct usb_serial_port *port) { } |
---|
| 2229 | + |
---|
| 2230 | +#endif /* CONFIG_GPIOLIB */ |
---|
| 2231 | + |
---|
1795 | 2232 | /* |
---|
1796 | 2233 | * *************************************************************************** |
---|
1797 | 2234 | * FTDI driver specific functions |
---|
.. | .. |
---|
1820 | 2257 | { |
---|
1821 | 2258 | struct ftdi_private *priv; |
---|
1822 | 2259 | const struct ftdi_sio_quirk *quirk = usb_get_serial_data(port->serial); |
---|
1823 | | - |
---|
| 2260 | + int result; |
---|
1824 | 2261 | |
---|
1825 | 2262 | priv = kzalloc(sizeof(struct ftdi_private), GFP_KERNEL); |
---|
1826 | 2263 | if (!priv) |
---|
.. | .. |
---|
1839 | 2276 | priv->latency = 16; |
---|
1840 | 2277 | write_latency_timer(port); |
---|
1841 | 2278 | create_sysfs_attrs(port); |
---|
| 2279 | + |
---|
| 2280 | + result = ftdi_gpio_init(port); |
---|
| 2281 | + if (result < 0) { |
---|
| 2282 | + dev_err(&port->serial->interface->dev, |
---|
| 2283 | + "GPIO initialisation failed: %d\n", |
---|
| 2284 | + result); |
---|
| 2285 | + } |
---|
| 2286 | + |
---|
1842 | 2287 | return 0; |
---|
1843 | 2288 | } |
---|
1844 | 2289 | |
---|
.. | .. |
---|
1903 | 2348 | */ |
---|
1904 | 2349 | static int ftdi_jtag_probe(struct usb_serial *serial) |
---|
1905 | 2350 | { |
---|
1906 | | - struct usb_device *udev = serial->dev; |
---|
1907 | | - struct usb_interface *interface = serial->interface; |
---|
| 2351 | + struct usb_interface *intf = serial->interface; |
---|
| 2352 | + int ifnum = intf->cur_altsetting->desc.bInterfaceNumber; |
---|
1908 | 2353 | |
---|
1909 | | - if (interface == udev->actconfig->interface[0]) { |
---|
1910 | | - dev_info(&udev->dev, |
---|
1911 | | - "Ignoring serial port reserved for JTAG\n"); |
---|
| 2354 | + if (ifnum == 0) { |
---|
| 2355 | + dev_info(&intf->dev, "Ignoring interface reserved for JTAG\n"); |
---|
1912 | 2356 | return -ENODEV; |
---|
1913 | 2357 | } |
---|
1914 | 2358 | |
---|
.. | .. |
---|
1940 | 2384 | */ |
---|
1941 | 2385 | static int ftdi_stmclite_probe(struct usb_serial *serial) |
---|
1942 | 2386 | { |
---|
1943 | | - struct usb_device *udev = serial->dev; |
---|
1944 | | - struct usb_interface *interface = serial->interface; |
---|
| 2387 | + struct usb_interface *intf = serial->interface; |
---|
| 2388 | + int ifnum = intf->cur_altsetting->desc.bInterfaceNumber; |
---|
1945 | 2389 | |
---|
1946 | | - if (interface == udev->actconfig->interface[0] || |
---|
1947 | | - interface == udev->actconfig->interface[1]) { |
---|
1948 | | - dev_info(&udev->dev, "Ignoring serial port reserved for JTAG\n"); |
---|
| 2390 | + if (ifnum < 2) { |
---|
| 2391 | + dev_info(&intf->dev, "Ignoring interface reserved for JTAG\n"); |
---|
1949 | 2392 | return -ENODEV; |
---|
1950 | 2393 | } |
---|
1951 | 2394 | |
---|
.. | .. |
---|
1955 | 2398 | static int ftdi_sio_port_remove(struct usb_serial_port *port) |
---|
1956 | 2399 | { |
---|
1957 | 2400 | struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
| 2401 | + |
---|
| 2402 | + ftdi_gpio_remove(port); |
---|
1958 | 2403 | |
---|
1959 | 2404 | remove_sysfs_attrs(port); |
---|
1960 | 2405 | |
---|
.. | .. |
---|
2055 | 2500 | struct ftdi_private *priv, unsigned char *buf, int len) |
---|
2056 | 2501 | { |
---|
2057 | 2502 | unsigned char status; |
---|
| 2503 | + bool brkint = false; |
---|
2058 | 2504 | int i; |
---|
2059 | 2505 | char flag; |
---|
2060 | 2506 | |
---|
.. | .. |
---|
2106 | 2552 | */ |
---|
2107 | 2553 | flag = TTY_NORMAL; |
---|
2108 | 2554 | if (buf[1] & FTDI_RS_ERR_MASK) { |
---|
2109 | | - /* Break takes precedence over parity, which takes precedence |
---|
2110 | | - * over framing errors */ |
---|
2111 | | - if (buf[1] & FTDI_RS_BI) { |
---|
2112 | | - flag = TTY_BREAK; |
---|
| 2555 | + /* |
---|
| 2556 | + * Break takes precedence over parity, which takes precedence |
---|
| 2557 | + * over framing errors. Note that break is only associated |
---|
| 2558 | + * with the last character in the buffer and only when it's a |
---|
| 2559 | + * NUL. |
---|
| 2560 | + */ |
---|
| 2561 | + if (buf[1] & FTDI_RS_BI && buf[len - 1] == '\0') { |
---|
2113 | 2562 | port->icount.brk++; |
---|
2114 | | - usb_serial_handle_break(port); |
---|
2115 | | - } else if (buf[1] & FTDI_RS_PE) { |
---|
| 2563 | + brkint = true; |
---|
| 2564 | + } |
---|
| 2565 | + if (buf[1] & FTDI_RS_PE) { |
---|
2116 | 2566 | flag = TTY_PARITY; |
---|
2117 | 2567 | port->icount.parity++; |
---|
2118 | 2568 | } else if (buf[1] & FTDI_RS_FE) { |
---|
.. | .. |
---|
2128 | 2578 | |
---|
2129 | 2579 | port->icount.rx += len - 2; |
---|
2130 | 2580 | |
---|
2131 | | - if (port->port.console && port->sysrq) { |
---|
| 2581 | + if (brkint || port->sysrq) { |
---|
2132 | 2582 | for (i = 2; i < len; i++) { |
---|
| 2583 | + if (brkint && i == len - 1) { |
---|
| 2584 | + if (usb_serial_handle_break(port)) |
---|
| 2585 | + return len - 3; |
---|
| 2586 | + flag = TTY_BREAK; |
---|
| 2587 | + } |
---|
2133 | 2588 | if (usb_serial_handle_sysrq_char(port, buf[i])) |
---|
2134 | 2589 | continue; |
---|
2135 | 2590 | tty_insert_flip_char(&port->port, buf[i], flag); |
---|
.. | .. |
---|
2146 | 2601 | { |
---|
2147 | 2602 | struct usb_serial_port *port = urb->context; |
---|
2148 | 2603 | struct ftdi_private *priv = usb_get_serial_port_data(port); |
---|
2149 | | - char *data = (char *)urb->transfer_buffer; |
---|
| 2604 | + char *data = urb->transfer_buffer; |
---|
2150 | 2605 | int i; |
---|
2151 | 2606 | int len; |
---|
2152 | 2607 | int count = 0; |
---|
.. | .. |
---|
2477 | 2932 | void __user *argp = (void __user *)arg; |
---|
2478 | 2933 | |
---|
2479 | 2934 | switch (cmd) { |
---|
2480 | | - case TIOCGSERIAL: |
---|
2481 | | - return get_serial_info(port, argp); |
---|
2482 | | - case TIOCSSERIAL: |
---|
2483 | | - return set_serial_info(tty, port, argp); |
---|
2484 | 2935 | case TIOCSERGETLSR: |
---|
2485 | 2936 | return get_lsr_info(port, argp); |
---|
2486 | 2937 | default: |
---|