Skip to content

Commit d4303e0

Browse files
Vamshi Gajjelagregkh
authored andcommitted
serial: core: Clean up uart_update_timeout() function
Rename the variable size to temp and change its data type from unsigned int to u64 to avoid type casting in multiplication. Remove the intermediate variable frame_time and use temp instead to accommodate the nanoseconds(ns). port->frame_time is an unsigned int, therefore an explicit cast is used to improve readability. Having said this unsigned int is sufficinet to hold frame time duration in nanoseconds for all the standard baudrates. Consider 9600 baud, it takes 1/9600 seconds for one bit, for a total of 10 bits (start, 8-bit data, stop) 10/9600=1.04 ms for 1 byte transfer, frame_time here is 1041667ns. As baudrate increases frame_time decreases, say for 115200 baud it is 86806ns. To avoid costly 64-bit arithmetic we do not upconvert the type for variable frame_time as overflow happens for extremely low baudrates which are impractical and are not used in real-world applications. Signed-off-by: Vamshi Gajjela <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent cb86a33 commit d4303e0

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

drivers/tty/serial/serial_core.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,10 @@ void
410410
uart_update_timeout(struct uart_port *port, unsigned int cflag,
411411
unsigned int baud)
412412
{
413-
unsigned int size = tty_get_frame_size(cflag);
414-
u64 frame_time;
413+
u64 temp = tty_get_frame_size(cflag);
415414

416-
frame_time = (u64)size * NSEC_PER_SEC;
417-
port->frame_time = DIV64_U64_ROUND_UP(frame_time, baud);
415+
temp *= NSEC_PER_SEC;
416+
port->frame_time = (unsigned int)DIV64_U64_ROUND_UP(temp, baud);
418417
}
419418
EXPORT_SYMBOL(uart_update_timeout);
420419

0 commit comments

Comments
 (0)