summaryrefslogtreecommitdiff
path: root/drivers/tty/tty_ioctl.c
diff options
context:
space:
mode:
authorJiri Slaby <jslaby@suse.cz>2021-06-10 11:02:44 +0200
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2021-06-15 14:03:26 +0200
commit654ee49b7e0883e660be6e6e20876fc4cbdaadd1 (patch)
tree9e14c689a306e390ec13537316ad3e3166062c43 /drivers/tty/tty_ioctl.c
parentfb524360f52228201b56149a138369da505141a2 (diff)
tty: make tty_get_{char,frame}_size available
Many tty drivers contain code to compute bits count depending on termios cflags. So extract this code from serial core to two separate tty helper functions: * tty_get_char_size -- only size of a character, without flags, * tty_get_frame_size -- complete size of a frame including flags. In the next patch, calls to these new functions replace many copies of this code. Note that we accept only cflag as a parameter. That's because some callers like pch_uart_startup or sunsab_console_setup don't have at hand termios which we could pass around. Cc: Joe Perches <joe@perches.com> Cc: Johan Hovold <johan@kernel.org> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> Signed-off-by: Jiri Slaby <jslaby@suse.cz> Link: https://lore.kernel.org/r/20210610090247.2593-1-jslaby@suse.cz Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/tty/tty_ioctl.c')
-rw-r--r--drivers/tty/tty_ioctl.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/drivers/tty/tty_ioctl.c b/drivers/tty/tty_ioctl.c
index 75885d502749..507a25d692bb 100644
--- a/drivers/tty/tty_ioctl.c
+++ b/drivers/tty/tty_ioctl.c
@@ -280,6 +280,51 @@ int tty_termios_hw_change(const struct ktermios *a, const struct ktermios *b)
EXPORT_SYMBOL(tty_termios_hw_change);
/**
+ * tty_get_char_size - get size of a character
+ * @cflag: termios cflag value
+ *
+ * Get the size (in bits) of a character depending on @cflag's %CSIZE
+ * setting.
+ */
+unsigned char tty_get_char_size(unsigned int cflag)
+{
+ switch (cflag & CSIZE) {
+ case CS5:
+ return 5;
+ case CS6:
+ return 6;
+ case CS7:
+ return 7;
+ case CS8:
+ default:
+ return 8;
+ }
+}
+EXPORT_SYMBOL_GPL(tty_get_char_size);
+
+/**
+ * tty_get_frame_size - get size of a frame
+ * @cflag: termios cflag value
+ *
+ * Get the size (in bits) of a frame depending on @cflag's %CSIZE, %CSTOPB,
+ * and %PARENB setting. The result is a sum of character size, start and
+ * stop bits -- one bit each -- second stop bit (if set), and parity bit
+ * (if set).
+ */
+unsigned char tty_get_frame_size(unsigned int cflag)
+{
+ unsigned char bits = 2 + tty_get_char_size(cflag);
+
+ if (cflag & CSTOPB)
+ bits++;
+ if (cflag & PARENB)
+ bits++;
+
+ return bits;
+}
+EXPORT_SYMBOL_GPL(tty_get_frame_size);
+
+/**
* tty_set_termios - update termios values
* @tty: tty to update
* @new_termios: desired new value