tty: make use of tty_get_{char,frame}_size
authorJiri Slaby <jslaby@suse.cz>
Thu, 10 Jun 2021 09:02:47 +0000 (11:02 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 15 Jun 2021 12:03:27 +0000 (14:03 +0200)
In the previous patch, we introduced tty_get_char_size() and
tty_get_frame_size() for computing character and frame sizes,
respectively. Here, we make use of them in various tty drivers where
applicable.

The stats look nice: 12 insertions, 169 deletions.

Cc: Arnd Bergmann <arnd@arndb.de>
Cc: David Lin <dtwlin@gmail.com>
Cc: Johan Hovold <johan@kernel.org>
Cc: Alex Elder <elder@kernel.org>
Cc: Shawn Guo <shawnguo@kernel.org>
Cc: Sascha Hauer <s.hauer@pengutronix.de>
Cc: Andy Gross <agross@kernel.org>
Cc: Bjorn Andersson <bjorn.andersson@linaro.org>
Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Cc: Alexandre Torgue <alexandre.torgue@foss.st.com>
Cc: Oliver Neukum <oneukum@suse.com>
Acked-by: Alex Elder <elder@kernel.org>
Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Link: https://lore.kernel.org/r/20210610090247.2593-4-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
13 files changed:
drivers/char/pcmcia/synclink_cs.c
drivers/staging/greybus/uart.c
drivers/tty/serial/cpm_uart/cpm_uart_core.c
drivers/tty/serial/mxs-auart.c
drivers/tty/serial/qcom_geni_serial.c
drivers/tty/serial/sh-sci.c
drivers/tty/serial/stm32-usart.c
drivers/tty/synclink_gt.c
drivers/usb/class/cdc-acm.c
drivers/usb/serial/belkin_sa.c
drivers/usb/serial/cypress_m8.c
drivers/usb/serial/pl2303.c
drivers/usb/serial/whiteheat.c

index 9f7420b..6eaefea 100644 (file)
@@ -1419,13 +1419,7 @@ static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty)
 
        /* byte size and parity */
 
-       switch (cflag & CSIZE) {
-       case CS5: info->params.data_bits = 5; break;
-       case CS6: info->params.data_bits = 6; break;
-       case CS7: info->params.data_bits = 7; break;
-       case CS8: info->params.data_bits = 8; break;
-       default:  info->params.data_bits = 7; break;
-       }
+       info->params.data_bits = tty_get_char_size(cflag);
 
        if (cflag & CSTOPB)
                info->params.stop_bits = 2;
index ccfaa0f..73f01ed 100644 (file)
@@ -494,21 +494,7 @@ static void gb_tty_set_termios(struct tty_struct *tty,
                                (termios->c_cflag & PARODD ? 1 : 2) +
                                (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
 
-       switch (termios->c_cflag & CSIZE) {
-       case CS5:
-               newline.data_bits = 5;
-               break;
-       case CS6:
-               newline.data_bits = 6;
-               break;
-       case CS7:
-               newline.data_bits = 7;
-               break;
-       case CS8:
-       default:
-               newline.data_bits = 8;
-               break;
-       }
+       newline.data_bits = tty_get_char_size(termios->c_cflag);
 
        /* FIXME: needs to clear unsupported bits in the termios */
        gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
index 58aaa53..c719aa2 100644 (file)
@@ -524,24 +524,7 @@ static void cpm_uart_set_termios(struct uart_port *port,
        scval = 0;
 
        /* byte size */
-       switch (termios->c_cflag & CSIZE) {
-       case CS5:
-               bits = 5;
-               break;
-       case CS6:
-               bits = 6;
-               break;
-       case CS7:
-               bits = 7;
-               break;
-       case CS8:
-               bits = 8;
-               break;
-               /* Never happens, but GCC is too dumb to figure it out */
-       default:
-               bits = 8;
-               break;
-       }
+       bits = tty_get_char_size(termios->c_cflag);
        sbits = bits - 5;
 
        if (termios->c_cflag & CSTOPB) {
index 7b4b6bb..ac45f33 100644 (file)
@@ -962,7 +962,7 @@ static void mxs_auart_settermios(struct uart_port *u,
                                 struct ktermios *old)
 {
        struct mxs_auart_port *s = to_auart_port(u);
-       u32 bm, ctrl, ctrl2, div;
+       u32 ctrl, ctrl2, div;
        unsigned int cflag, baud, baud_min, baud_max;
 
        cflag = termios->c_cflag;
@@ -970,25 +970,7 @@ static void mxs_auart_settermios(struct uart_port *u,
        ctrl = AUART_LINECTRL_FEN;
        ctrl2 = mxs_read(s, REG_CTRL2);
 
-       /* byte size */
-       switch (cflag & CSIZE) {
-       case CS5:
-               bm = 5;
-               break;
-       case CS6:
-               bm = 6;
-               break;
-       case CS7:
-               bm = 7;
-               break;
-       case CS8:
-               bm = 8;
-               break;
-       default:
-               return;
-       }
-
-       ctrl |= AUART_LINECTRL_WLEN(bm);
+       ctrl |= AUART_LINECTRL_WLEN(tty_get_char_size(cflag));
 
        /* parity */
        if (cflag & PARENB) {
index 463f84a..379ab15 100644 (file)
@@ -1050,21 +1050,7 @@ static void qcom_geni_serial_set_termios(struct uart_port *uport,
        }
 
        /* bits per char */
-       switch (termios->c_cflag & CSIZE) {
-       case CS5:
-               bits_per_char = 5;
-               break;
-       case CS6:
-               bits_per_char = 6;
-               break;
-       case CS7:
-               bits_per_char = 7;
-               break;
-       case CS8:
-       default:
-               bits_per_char = 8;
-               break;
-       }
+       bits_per_char = tty_get_char_size(termios->c_cflag);
 
        /* stop bits */
        if (termios->c_cflag & CSTOPB)
index aabe66c..07eb562 100644 (file)
@@ -2500,25 +2500,7 @@ done:
        uart_update_timeout(port, termios->c_cflag, baud);
 
        /* byte size and parity */
-       switch (termios->c_cflag & CSIZE) {
-       case CS5:
-               bits = 7;
-               break;
-       case CS6:
-               bits = 8;
-               break;
-       case CS7:
-               bits = 9;
-               break;
-       default:
-               bits = 10;
-               break;
-       }
-
-       if (termios->c_cflag & CSTOPB)
-               bits++;
-       if (termios->c_cflag & PARENB)
-               bits++;
+       bits = tty_get_frame_size(termios->c_cflag);
 
        if (sci_getreg(port, SEMR)->size)
                serial_port_out(port, SEMR, 0);
index cd6adb9..ef793b3 100644 (file)
@@ -718,36 +718,6 @@ static void stm32_usart_shutdown(struct uart_port *port)
        free_irq(port->irq, port);
 }
 
-static unsigned int stm32_usart_get_databits(struct ktermios *termios)
-{
-       unsigned int bits;
-
-       tcflag_t cflag = termios->c_cflag;
-
-       switch (cflag & CSIZE) {
-       /*
-        * CSIZE settings are not necessarily supported in hardware.
-        * CSIZE unsupported configurations are handled here to set word length
-        * to 8 bits word as default configuration and to print debug message.
-        */
-       case CS5:
-               bits = 5;
-               break;
-       case CS6:
-               bits = 6;
-               break;
-       case CS7:
-               bits = 7;
-               break;
-       /* default including CS8 */
-       default:
-               bits = 8;
-               break;
-       }
-
-       return bits;
-}
-
 static void stm32_usart_set_termios(struct uart_port *port,
                                    struct ktermios *termios,
                                    struct ktermios *old)
@@ -805,7 +775,7 @@ static void stm32_usart_set_termios(struct uart_port *port,
        if (cflag & CSTOPB)
                cr2 |= USART_CR2_STOP_2B;
 
-       bits = stm32_usart_get_databits(termios);
+       bits = tty_get_char_size(cflag);
        stm32_port->rdr_mask = (BIT(bits) - 1);
 
        if (cflag & PARENB) {
index cf87dc6..5bb928b 100644 (file)
@@ -2465,14 +2465,7 @@ static void change_params(struct slgt_info *info)
 
        /* byte size and parity */
 
-       switch (cflag & CSIZE) {
-       case CS5: info->params.data_bits = 5; break;
-       case CS6: info->params.data_bits = 6; break;
-       case CS7: info->params.data_bits = 7; break;
-       case CS8: info->params.data_bits = 8; break;
-       default:  info->params.data_bits = 7; break;
-       }
-
+       info->params.data_bits = tty_get_char_size(cflag);
        info->params.stop_bits = (cflag & CSTOPB) ? 2 : 1;
 
        if (cflag & PARENB)
index 81199ef..c9954eb 100644 (file)
@@ -1056,21 +1056,8 @@ static void acm_tty_set_termios(struct tty_struct *tty,
        newline.bParityType = termios->c_cflag & PARENB ?
                                (termios->c_cflag & PARODD ? 1 : 2) +
                                (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
-       switch (termios->c_cflag & CSIZE) {
-       case CS5:
-               newline.bDataBits = 5;
-               break;
-       case CS6:
-               newline.bDataBits = 6;
-               break;
-       case CS7:
-               newline.bDataBits = 7;
-               break;
-       case CS8:
-       default:
-               newline.bDataBits = 8;
-               break;
-       }
+       newline.bDataBits = tty_get_char_size(termios->c_cflag);
+
        /* FIXME: Needs to clear unsupported bits in the termios */
        acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
 
index ed9193f..8107e4b 100644 (file)
@@ -356,25 +356,7 @@ static void belkin_sa_set_termios(struct tty_struct *tty,
 
        /* set the number of data bits */
        if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
-               switch (cflag & CSIZE) {
-               case CS5:
-                       urb_value = BELKIN_SA_DATA_BITS(5);
-                       break;
-               case CS6:
-                       urb_value = BELKIN_SA_DATA_BITS(6);
-                       break;
-               case CS7:
-                       urb_value = BELKIN_SA_DATA_BITS(7);
-                       break;
-               case CS8:
-                       urb_value = BELKIN_SA_DATA_BITS(8);
-                       break;
-               default:
-                       dev_dbg(&port->dev,
-                               "CSIZE was not CS5-CS8, using default of 8\n");
-                       urb_value = BELKIN_SA_DATA_BITS(8);
-                       break;
-               }
+               urb_value = BELKIN_SA_DATA_BITS(tty_get_char_size(cflag));
                if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
                        dev_err(&port->dev, "Set data bits error\n");
        }
index 4dea3ec..55f23df 100644 (file)
@@ -887,23 +887,8 @@ static void cypress_set_termios(struct tty_struct *tty,
        } else
                parity_enable = parity_type = 0;
 
-       switch (cflag & CSIZE) {
-       case CS5:
-               data_bits = 5;
-               break;
-       case CS6:
-               data_bits = 6;
-               break;
-       case CS7:
-               data_bits = 7;
-               break;
-       case CS8:
-               data_bits = 8;
-               break;
-       default:
-               dev_err(dev, "%s - CSIZE was set, but not CS5-CS8\n", __func__);
-               data_bits = 8;
-       }
+       data_bits = tty_get_char_size(cflag);
+
        spin_lock_irqsave(&priv->lock, flags);
        oldlines = priv->line_control;
        if ((cflag & CBAUD) == B0) {
index 940050c..2f2f504 100644 (file)
@@ -789,20 +789,7 @@ static void pl2303_set_termios(struct tty_struct *tty,
 
        pl2303_get_line_request(port, buf);
 
-       switch (C_CSIZE(tty)) {
-       case CS5:
-               buf[6] = 5;
-               break;
-       case CS6:
-               buf[6] = 6;
-               break;
-       case CS7:
-               buf[6] = 7;
-               break;
-       default:
-       case CS8:
-               buf[6] = 8;
-       }
+       buf[6] = tty_get_char_size(tty->termios.c_cflag);
        dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
 
        /* For reference buf[0]:buf[3] baud rate value */
index 5116ed9..da65d14 100644 (file)
@@ -625,14 +625,7 @@ static void firm_setup_port(struct tty_struct *tty)
 
        port_settings.port = port->port_number + 1;
 
-       /* get the byte size */
-       switch (cflag & CSIZE) {
-       case CS5:       port_settings.bits = 5;   break;
-       case CS6:       port_settings.bits = 6;   break;
-       case CS7:       port_settings.bits = 7;   break;
-       default:
-       case CS8:       port_settings.bits = 8;   break;
-       }
+       port_settings.bits = tty_get_char_size(cflag);
        dev_dbg(dev, "%s - data bits = %d\n", __func__, port_settings.bits);
 
        /* determine the parity */