[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <tencent_50535E8627177335AEA311C5DC0A54609008@qq.com>
Date: Thu, 6 Mar 2025 11:00:32 +0800
From: xiaopeitux@...mail.com
To: gregkh@...uxfoundation.org,
linux-kernel@...r.kernel.org,
linux-serial@...r.kernel.org
Cc: xiaopei01 <xiaopei01@...inos.cn>
Subject: [PATCH] serial: 8250: Optimize port function assignment with generic macro
From: xiaopei01 <xiaopei01@...inos.cn>
Refactor repetitive conditional function pointer assignments using a
generic macro ASSIGN_IF_EXIST. This consolidates 15+ conditional
checks into a consistent pattern while maintaining type safety.
Signed-off-by: xiaopei01 <xiaopei01@...inos.cn>
---
drivers/tty/serial/8250/8250_core.c | 52 ++++++++++++++++---------------------
1 file changed, 22 insertions(+), 30 deletions(-)
diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/8250_core.c
index 6f676bb..6f305e9 100644
--- a/drivers/tty/serial/8250/8250_core.c
+++ b/drivers/tty/serial/8250/8250_core.c
@@ -777,38 +777,30 @@ int serial8250_register_8250_port(const struct uart_8250_port *up)
serial8250_set_defaults(uart);
+ #define ASSIGN_IF_EXIST(dest, src, member) \
+ do { \
+ if ((src)->member) \
+ (dest)->member = (src)->member; \
+ } while (0)
+
/* Possibly override default I/O functions. */
- if (up->port.serial_in)
- uart->port.serial_in = up->port.serial_in;
- if (up->port.serial_out)
- uart->port.serial_out = up->port.serial_out;
- if (up->port.handle_irq)
- uart->port.handle_irq = up->port.handle_irq;
+ ASSIGN_IF_EXIST(&uart->port, &up->port, serial_in);
+ ASSIGN_IF_EXIST(&uart->port, &up->port, serial_out);
+ ASSIGN_IF_EXIST(&uart->port, &up->port, handle_irq);
/* Possibly override set_termios call */
- if (up->port.set_termios)
- uart->port.set_termios = up->port.set_termios;
- if (up->port.set_ldisc)
- uart->port.set_ldisc = up->port.set_ldisc;
- if (up->port.get_mctrl)
- uart->port.get_mctrl = up->port.get_mctrl;
- if (up->port.set_mctrl)
- uart->port.set_mctrl = up->port.set_mctrl;
- if (up->port.get_divisor)
- uart->port.get_divisor = up->port.get_divisor;
- if (up->port.set_divisor)
- uart->port.set_divisor = up->port.set_divisor;
- if (up->port.startup)
- uart->port.startup = up->port.startup;
- if (up->port.shutdown)
- uart->port.shutdown = up->port.shutdown;
- if (up->port.pm)
- uart->port.pm = up->port.pm;
- if (up->port.handle_break)
- uart->port.handle_break = up->port.handle_break;
- if (up->dl_read)
- uart->dl_read = up->dl_read;
- if (up->dl_write)
- uart->dl_write = up->dl_write;
+ ASSIGN_IF_EXIST(&uart->port, &up->port, set_termios);
+ ASSIGN_IF_EXIST(&uart->port, &up->port, set_ldisc);
+ ASSIGN_IF_EXIST(&uart->port, &up->port, get_mctrl);
+ ASSIGN_IF_EXIST(&uart->port, &up->port, set_mctrl);
+ ASSIGN_IF_EXIST(&uart->port, &up->port, get_divisor);
+ ASSIGN_IF_EXIST(&uart->port, &up->port, set_divisor);
+ ASSIGN_IF_EXIST(&uart->port, &up->port, startup);
+ ASSIGN_IF_EXIST(&uart->port, &up->port, shutdown);
+ ASSIGN_IF_EXIST(&uart->port, &up->port, pm);
+ ASSIGN_IF_EXIST(&uart->port, &up->port, handle_break);
+
+ ASSIGN_IF_EXIST(uart, up, dl_read);
+ ASSIGN_IF_EXIST(uart, up, dl_write);
if (uart->port.type != PORT_8250_CIR) {
if (uart_console_registered(&uart->port))
--
2.7.4
Powered by blists - more mailing lists