hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/usb/serial/ftdi_sio.c
....@@ -10,7 +10,7 @@
1010 * Copyright (C) 2002
1111 * Kuba Ober (kuba@mareimbrium.org)
1212 *
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
1414 * driver
1515 *
1616 * See http://ftdi-usb-sio.sourceforge.net for up to date testing info
....@@ -39,6 +39,7 @@
3939 #include <linux/uaccess.h>
4040 #include <linux/usb.h>
4141 #include <linux/serial.h>
42
+#include <linux/gpio/driver.h>
4243 #include <linux/usb/serial.h>
4344 #include "ftdi_sio.h"
4445 #include "ftdi_sio_ids.h"
....@@ -72,6 +73,15 @@
7273 unsigned int latency; /* latency setting in use */
7374 unsigned short max_packet_size;
7475 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
7585 };
7686
7787 /* struct ftdi_sio_quirk is used by devices requiring special attention. */
....@@ -1013,6 +1023,9 @@
10131023 { USB_DEVICE(FTDI_VID, CHETCO_SEASMART_DISPLAY_PID) },
10141024 { USB_DEVICE(FTDI_VID, CHETCO_SEASMART_LITE_PID) },
10151025 { 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) },
10161029 /* ICP DAS I-756xU devices */
10171030 { USB_DEVICE(ICPDAS_VID, ICPDAS_I7560U_PID) },
10181031 { USB_DEVICE(ICPDAS_VID, ICPDAS_I7561U_PID) },
....@@ -1032,6 +1045,8 @@
10321045 /* IDS GmbH devices */
10331046 { USB_DEVICE(IDS_VID, IDS_SI31A_PID) },
10341047 { USB_DEVICE(IDS_VID, IDS_CM31A_PID) },
1048
+ /* Omron devices */
1049
+ { USB_DEVICE(OMRON_VID, OMRON_CS1W_CIF31_PID) },
10351050 /* U-Blox devices */
10361051 { USB_DEVICE(UBLOX_VID, UBLOX_C099F9P_ZED_PID) },
10371052 { USB_DEVICE(UBLOX_VID, UBLOX_C099F9P_ODIN_PID) },
....@@ -1080,6 +1095,10 @@
10801095 unsigned int set, unsigned int clear);
10811096 static int ftdi_ioctl(struct tty_struct *tty,
10821097 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);
10831102 static void ftdi_break_ctl(struct tty_struct *tty, int break_state);
10841103 static bool ftdi_tx_empty(struct usb_serial_port *port);
10851104 static int ftdi_get_modem_status(struct usb_serial_port *port,
....@@ -1116,6 +1135,8 @@
11161135 .tiocmiwait = usb_serial_generic_tiocmiwait,
11171136 .get_icount = usb_serial_generic_get_icount,
11181137 .ioctl = ftdi_ioctl,
1138
+ .get_serial = get_serial_info,
1139
+ .set_serial = set_serial_info,
11191140 .set_termios = ftdi_set_termios,
11201141 .break_ctl = ftdi_break_ctl,
11211142 .tx_empty = ftdi_tx_empty,
....@@ -1139,7 +1160,7 @@
11391160 {
11401161 unsigned short int divisor;
11411162 /* divisor shifted 3 bits to the left */
1142
- int divisor3 = base / 2 / baud;
1163
+ int divisor3 = DIV_ROUND_CLOSEST(base, 2 * baud);
11431164 if ((divisor3 & 0x7) == 7)
11441165 divisor3++; /* round x.7/8 up to x+1 */
11451166 divisor = divisor3 >> 3;
....@@ -1165,7 +1186,7 @@
11651186 static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 };
11661187 u32 divisor;
11671188 /* divisor shifted 3 bits to the left */
1168
- int divisor3 = base / 2 / baud;
1189
+ int divisor3 = DIV_ROUND_CLOSEST(base, 2 * baud);
11691190 divisor = divisor3 >> 3;
11701191 divisor |= (u32)divfrac[divisor3 & 0x7] << 14;
11711192 /* Deal with special cases for highest baud rates. */
....@@ -1188,7 +1209,7 @@
11881209 int divisor3;
11891210
11901211 /* 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);
11921213
11931214 divisor = divisor3 >> 3;
11941215 divisor |= (u32)divfrac[divisor3 & 0x7] << 14;
....@@ -1299,8 +1320,7 @@
12991320 case 38400: div_value = ftdi_sio_b38400; break;
13001321 case 57600: div_value = ftdi_sio_b57600; break;
13011322 case 115200: div_value = ftdi_sio_b115200; break;
1302
- } /* baud */
1303
- if (div_value == 0) {
1323
+ default:
13041324 dev_dbg(dev, "%s - Baudrate (%d) requested is not supported\n",
13051325 __func__, baud);
13061326 div_value = ftdi_sio_b9600;
....@@ -1469,30 +1489,24 @@
14691489 return 0;
14701490 }
14711491
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)
14741494 {
1495
+ struct usb_serial_port *port = tty->driver_data;
14751496 struct ftdi_private *priv = usb_get_serial_port_data(port);
1476
- struct serial_struct tmp;
14771497
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;
14841501 return 0;
14851502 }
14861503
14871504 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)
14891506 {
1507
+ struct usb_serial_port *port = tty->driver_data;
14901508 struct ftdi_private *priv = usb_get_serial_port_data(port);
1491
- struct serial_struct new_serial;
14921509 struct ftdi_private old_priv;
1493
-
1494
- if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
1495
- return -EFAULT;
14961510
14971511 mutex_lock(&priv->cfg_lock);
14981512 old_priv = *priv;
....@@ -1500,17 +1514,17 @@
15001514 /* Do error checking and permission checking */
15011515
15021516 if (!capable(CAP_SYS_ADMIN)) {
1503
- if ((new_serial.flags ^ priv->flags) & ~ASYNC_USR_MASK) {
1517
+ if ((ss->flags ^ priv->flags) & ~ASYNC_USR_MASK) {
15041518 mutex_unlock(&priv->cfg_lock);
15051519 return -EPERM;
15061520 }
15071521 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;
15101524 goto check_and_exit;
15111525 }
15121526
1513
- if (new_serial.baud_base != priv->baud_base) {
1527
+ if (ss->baud_base != priv->baud_base) {
15141528 mutex_unlock(&priv->cfg_lock);
15151529 return -EINVAL;
15161530 }
....@@ -1518,8 +1532,8 @@
15181532 /* Make the changes - these are privileged changes! */
15191533
15201534 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;
15231537
15241538 check_and_exit:
15251539 write_latency_timer(port);
....@@ -1533,10 +1547,8 @@
15331547 dev_warn_ratelimited(&port->dev, "use of SPD flags is deprecated\n");
15341548
15351549 change_speed(tty, port);
1536
- mutex_unlock(&priv->cfg_lock);
15371550 }
1538
- else
1539
- mutex_unlock(&priv->cfg_lock);
1551
+ mutex_unlock(&priv->cfg_lock);
15401552 return 0;
15411553 }
15421554
....@@ -1572,7 +1584,8 @@
15721584 dev_dbg(&port->dev, "%s: bcdDevice = 0x%x, bNumInterfaces = %u\n", __func__,
15731585 version, interfaces);
15741586 if (interfaces > 1) {
1575
- int inter;
1587
+ struct usb_interface *intf = serial->interface;
1588
+ int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
15761589
15771590 /* Multiple interfaces.*/
15781591 if (version == 0x0800) {
....@@ -1587,16 +1600,15 @@
15871600 priv->chip_type = FT2232C;
15881601
15891602 /* Determine interface code. */
1590
- inter = serial->interface->altsetting->desc.bInterfaceNumber;
1591
- if (inter == 0) {
1603
+ if (ifnum == 0)
15921604 priv->interface = INTERFACE_A;
1593
- } else if (inter == 1) {
1605
+ else if (ifnum == 1)
15941606 priv->interface = INTERFACE_B;
1595
- } else if (inter == 2) {
1607
+ else if (ifnum == 2)
15961608 priv->interface = INTERFACE_C;
1597
- } else if (inter == 3) {
1609
+ else if (ifnum == 3)
15981610 priv->interface = INTERFACE_D;
1599
- }
1611
+
16001612 /* BM-type devices have a bug where bcdDevice gets set
16011613 * to 0x200 when iSerialNumber is 0. */
16021614 if (version < 0x500) {
....@@ -1792,6 +1804,431 @@
17921804
17931805 }
17941806
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
+
17952232 /*
17962233 * ***************************************************************************
17972234 * FTDI driver specific functions
....@@ -1820,7 +2257,7 @@
18202257 {
18212258 struct ftdi_private *priv;
18222259 const struct ftdi_sio_quirk *quirk = usb_get_serial_data(port->serial);
1823
-
2260
+ int result;
18242261
18252262 priv = kzalloc(sizeof(struct ftdi_private), GFP_KERNEL);
18262263 if (!priv)
....@@ -1839,6 +2276,14 @@
18392276 priv->latency = 16;
18402277 write_latency_timer(port);
18412278 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
+
18422287 return 0;
18432288 }
18442289
....@@ -1903,12 +2348,11 @@
19032348 */
19042349 static int ftdi_jtag_probe(struct usb_serial *serial)
19052350 {
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;
19082353
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");
19122356 return -ENODEV;
19132357 }
19142358
....@@ -1940,12 +2384,11 @@
19402384 */
19412385 static int ftdi_stmclite_probe(struct usb_serial *serial)
19422386 {
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;
19452389
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");
19492392 return -ENODEV;
19502393 }
19512394
....@@ -1955,6 +2398,8 @@
19552398 static int ftdi_sio_port_remove(struct usb_serial_port *port)
19562399 {
19572400 struct ftdi_private *priv = usb_get_serial_port_data(port);
2401
+
2402
+ ftdi_gpio_remove(port);
19582403
19592404 remove_sysfs_attrs(port);
19602405
....@@ -2055,6 +2500,7 @@
20552500 struct ftdi_private *priv, unsigned char *buf, int len)
20562501 {
20572502 unsigned char status;
2503
+ bool brkint = false;
20582504 int i;
20592505 char flag;
20602506
....@@ -2106,13 +2552,17 @@
21062552 */
21072553 flag = TTY_NORMAL;
21082554 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') {
21132562 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) {
21162566 flag = TTY_PARITY;
21172567 port->icount.parity++;
21182568 } else if (buf[1] & FTDI_RS_FE) {
....@@ -2128,8 +2578,13 @@
21282578
21292579 port->icount.rx += len - 2;
21302580
2131
- if (port->port.console && port->sysrq) {
2581
+ if (brkint || port->sysrq) {
21322582 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
+ }
21332588 if (usb_serial_handle_sysrq_char(port, buf[i]))
21342589 continue;
21352590 tty_insert_flip_char(&port->port, buf[i], flag);
....@@ -2146,7 +2601,7 @@
21462601 {
21472602 struct usb_serial_port *port = urb->context;
21482603 struct ftdi_private *priv = usb_get_serial_port_data(port);
2149
- char *data = (char *)urb->transfer_buffer;
2604
+ char *data = urb->transfer_buffer;
21502605 int i;
21512606 int len;
21522607 int count = 0;
....@@ -2477,10 +2932,6 @@
24772932 void __user *argp = (void __user *)arg;
24782933
24792934 switch (cmd) {
2480
- case TIOCGSERIAL:
2481
- return get_serial_info(port, argp);
2482
- case TIOCSSERIAL:
2483
- return set_serial_info(tty, port, argp);
24842935 case TIOCSERGETLSR:
24852936 return get_lsr_info(port, argp);
24862937 default: