From nobody Thu Dec 18 21:45:20 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21BD2CA0EC3 for ; Tue, 12 Sep 2023 11:05:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234288AbjILLFK (ORCPT ); Tue, 12 Sep 2023 07:05:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234555AbjILLEX (ORCPT ); Tue, 12 Sep 2023 07:04:23 -0400 Received: from muru.com (muru.com [72.249.23.125]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E190A9F; Tue, 12 Sep 2023 04:04:14 -0700 (PDT) Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 3BF168106; Tue, 12 Sep 2023 11:04:12 +0000 (UTC) From: Tony Lindgren To: Greg Kroah-Hartman , Jiri Slaby , Petr Mladek , Sergey Senozhatsky , Steven Rostedt , John Ogness Cc: Andy Shevchenko , Dhruva Gole , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , Johan Hovold , Sebastian Andrzej Siewior , Vignesh Raghavendra , linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org Subject: [PATCH v2 1/3] printk: Constify name for add_preferred_console() Date: Tue, 12 Sep 2023 14:03:43 +0300 Message-ID: <20230912110350.14482-2-tony@atomide.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230912110350.14482-1-tony@atomide.com> References: <20230912110350.14482-1-tony@atomide.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" While adding a preferred console handling for serial_core for serial port hardware based device addressing, Jiri suggested we constify name for add_preferred_console(). The gets copied anyways. This allows serial core to add a preferred console using serial drv->dev_name without copying it. Note that constifying options causes changes all over the place because of struct console for match(). Cc: John Ogness Cc: Petr Mladek Cc: Sergey Senozhatsky Cc: Steven Rostedt Suggested-by: Jiri Slaby Signed-off-by: Tony Lindgren Reviewed-by: Petr Mladek --- include/linux/console.h | 2 +- kernel/printk/printk.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h --- a/include/linux/console.h +++ b/include/linux/console.h @@ -337,7 +337,7 @@ enum con_flush_mode { CONSOLE_REPLAY_ALL, }; =20 -extern int add_preferred_console(char *name, int idx, char *options); +extern int add_preferred_console(const char *name, int idx, char *options); extern void console_force_preferred_locked(struct console *con); extern void register_console(struct console *); extern int unregister_console(struct console *); diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2400,7 +2400,7 @@ static void set_user_specified(struct console_cmdline= *c, bool user_specified) console_set_on_cmdline =3D 1; } =20 -static int __add_preferred_console(char *name, int idx, char *options, +static int __add_preferred_console(const char *name, int idx, char *option= s, char *brl_options, bool user_specified) { struct console_cmdline *c; @@ -2509,7 +2509,7 @@ __setup("console=3D", console_setup); * commonly to provide a default console (ie from PROM variables) when * the user has not supplied one. */ -int add_preferred_console(char *name, int idx, char *options) +int add_preferred_console(const char *name, int idx, char *options) { return __add_preferred_console(name, idx, options, NULL, false); } --=20 2.42.0 From nobody Thu Dec 18 21:45:20 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AAC9ECA0EC3 for ; Tue, 12 Sep 2023 11:05:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234126AbjILLFB (ORCPT ); Tue, 12 Sep 2023 07:05:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233867AbjILLE0 (ORCPT ); Tue, 12 Sep 2023 07:04:26 -0400 Received: from muru.com (muru.com [72.249.23.125]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 94171B9; Tue, 12 Sep 2023 04:04:22 -0700 (PDT) Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 6587D80FC; Tue, 12 Sep 2023 11:04:20 +0000 (UTC) From: Tony Lindgren To: Greg Kroah-Hartman , Jiri Slaby Cc: Andy Shevchenko , Dhruva Gole , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , John Ogness , Johan Hovold , Sebastian Andrzej Siewior , Vignesh Raghavendra , linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org Subject: [PATCH v2 2/3] serial: core: Add support for DEVNAME:0.0 style naming for kernel console Date: Tue, 12 Sep 2023 14:03:44 +0300 Message-ID: <20230912110350.14482-3-tony@atomide.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230912110350.14482-1-tony@atomide.com> References: <20230912110350.14482-1-tony@atomide.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" We can now add hardware based addressing to serial ports. Starting with commit 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM"), and all the related fixes to this commit, the serial core now knows to which serial port controller the ports are connected. The serial ports can be addressed with DEVNAME:0.0 style naming. The names are something like 00:04:0.0 for a serial port on qemu, and something like 2800000.serial:0.0 on platform device using systems like ARM64 for example. The DEVNAME is the unique serial port hardware controller device name, AKA the name for port->dev. The 0.0 are the serial core controller id and port id. Typically 0.0 are used for each controller and port instance unless the serial port hardware controller has multiple controllers or ports. Using DEVNAME:0.0 style naming actually solves two long term issues for addressing the serial ports: 1. According to Andy Shevchenko, using DEVNAME:0.0 style naming fixes an issue where depending on the BIOS settings, the kernel serial port ttyS instance number may change if HSUART is enabled 2. Device tree using architectures no longer necessarily need to specify aliases to find a specific serial port, and we can just allocate the ttyS instance numbers dynamically in whatever probe order To do this, we need a custom init time parser for the console=3D command line option as printk already handles parsing it with console_setup(). Also early_param() gets handled by console_setup() if "console" and "earlycon" are used. Signed-off-by: Tony Lindgren --- drivers/tty/serial/Makefile | 3 + drivers/tty/serial/serial_base.h | 11 +++ drivers/tty/serial/serial_base_con.c | 133 +++++++++++++++++++++++++++ drivers/tty/serial/serial_core.c | 4 + 4 files changed, 151 insertions(+) create mode 100644 drivers/tty/serial/serial_base_con.c diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile --- a/drivers/tty/serial/Makefile +++ b/drivers/tty/serial/Makefile @@ -3,6 +3,9 @@ # Makefile for the kernel serial device drivers. # =20 +# Parse kernel command line consoles before the serial drivers probe +obj-$(CONFIG_SERIAL_CORE_CONSOLE) +=3D serial_base_con.o + obj-$(CONFIG_SERIAL_CORE) +=3D serial_base.o serial_base-y :=3D serial_core.o serial_base_bus.o serial_ctrl.o serial_po= rt.o =20 diff --git a/drivers/tty/serial/serial_base.h b/drivers/tty/serial/serial_b= ase.h --- a/drivers/tty/serial/serial_base.h +++ b/drivers/tty/serial/serial_base.h @@ -45,3 +45,14 @@ void serial_ctrl_unregister_port(struct uart_driver *drv= , struct uart_port *port =20 int serial_core_register_port(struct uart_driver *drv, struct uart_port *p= ort); void serial_core_unregister_port(struct uart_driver *drv, struct uart_port= *port); + +#ifdef CONFIG_SERIAL_CORE_CONSOLE +int serial_base_add_preferred_console(struct uart_driver *drv, + struct uart_port *port); +#else +static inline int serial_base_add_preferred_console(struct uart_driver *dr= v, + struct uart_port *port) +{ + return 0; +} +#endif diff --git a/drivers/tty/serial/serial_base_con.c b/drivers/tty/serial/seri= al_base_con.c new file mode 100644 --- /dev/null +++ b/drivers/tty/serial/serial_base_con.c @@ -0,0 +1,133 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Serial base console options handling + * + * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/ + * Author: Tony Lindgren + */ + +#include +#include +#include +#include +#include + +#include "serial_base.h" + +static LIST_HEAD(serial_base_consoles); + +struct serial_base_console { + struct list_head node; + char *name; + char *opt; +}; + +/* + * Adds a preferred console for a serial port if console=3DDEVNAME:0.0 + * style addressing is used for the kernel command line. Translates + * from DEVNAME:0.0 to port->dev_name such as ttyS. Duplicates are + * ignored by add_preferred_console(). + */ +int serial_base_add_preferred_console(struct uart_driver *drv, + struct uart_port *port) +{ + struct serial_base_console *entry; + char *port_match; + + port_match =3D kasprintf(GFP_KERNEL, "%s:%i.%i", dev_name(port->dev), + port->ctrl_id, port->port_id); + if (!port_match) + return -ENOMEM; + + list_for_each_entry(entry, &serial_base_consoles, node) { + if (!strcmp(port_match, entry->name)) { + add_preferred_console(drv->dev_name, port->line, + entry->opt); + break; + } + } + + kfree(port_match); + + return 0; +} +EXPORT_SYMBOL_GPL(serial_base_add_preferred_console); + +/* Adds a command line console to the list of consoles for driver probe ti= me */ +static int __init serial_base_add_con(char *name, char *opt) +{ + struct serial_base_console *con; + + con =3D kzalloc(sizeof(*con), GFP_KERNEL); + if (!con) + return -ENOMEM; + + con->name =3D kstrdup(name, GFP_KERNEL); + if (!con->name) + goto free_con; + + if (opt) { + con->opt =3D kstrdup(opt, GFP_KERNEL); + if (!con->name) + goto free_name; + } + + list_add_tail(&con->node, &serial_base_consoles); + + return 0; + +free_name: + kfree(con->name); + +free_con: + kfree(con); + + return -ENOMEM; +} + +/* Parse console name and options */ +static int __init serial_base_parse_one(char *param, char *val, + const char *unused, void *arg) +{ + char *opt; + + if (strcmp(param, "console")) + return 0; + + if (!val) + return 0; + + opt =3D strchr(val, ','); + if (opt) { + opt[0] =3D '\0'; + opt++; + } + + if (!strlen(val)) + return 0; + + return serial_base_add_con(val, opt); +} + +/* + * The "console=3D" option is handled by console_setup() in printk. We can= 't use + * early_param() as do_early_param() checks for "console" and "earlycon" o= ptions + * so console_setup() potentially handles console also early. Use parse_ar= gs(). + */ +static int __init serial_base_opts_init(void) +{ + char *command_line; + + command_line =3D kstrdup(boot_command_line, GFP_KERNEL); + if (!command_line) + return -ENOMEM; + + parse_args("Setting serial core console", command_line, + NULL, 0, -1, -1, NULL, serial_base_parse_one); + + kfree(command_line); + + return 0; +} + +arch_initcall(serial_base_opts_init); diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_c= ore.c --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -3358,6 +3358,10 @@ int serial_core_register_port(struct uart_driver *dr= v, struct uart_port *port) if (ret) goto err_unregister_ctrl_dev; =20 + ret =3D serial_base_add_preferred_console(drv, port); + if (ret) + goto err_unregister_port_dev; + ret =3D serial_core_add_one_port(drv, port); if (ret) goto err_unregister_port_dev; --=20 2.42.0 From nobody Thu Dec 18 21:45:20 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AAC63CA0ECF for ; Tue, 12 Sep 2023 11:04:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234286AbjILLEt (ORCPT ); Tue, 12 Sep 2023 07:04:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233899AbjILLEb (ORCPT ); Tue, 12 Sep 2023 07:04:31 -0400 Received: from muru.com (muru.com [72.249.23.125]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 72DFF10E2; Tue, 12 Sep 2023 04:04:27 -0700 (PDT) Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 461AF80FC; Tue, 12 Sep 2023 11:04:25 +0000 (UTC) From: Tony Lindgren To: Greg Kroah-Hartman , Jiri Slaby Cc: Andy Shevchenko , Dhruva Gole , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , John Ogness , Johan Hovold , Sebastian Andrzej Siewior , Vignesh Raghavendra , linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org Subject: [PATCH v2 3/3] serial: core: Add sysfs links for serial core port instances for ttys Date: Tue, 12 Sep 2023 14:03:45 +0300 Message-ID: <20230912110350.14482-4-tony@atomide.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230912110350.14482-1-tony@atomide.com> References: <20230912110350.14482-1-tony@atomide.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Let's allow the userspace to find out the ttyS style name for a serial core port device if a tty exists. This can be done with: $ grep DEVNAME /sys/bus/serial-base/devices/*/tty/uevent /sys/bus/serial-base/devices/00:04:0.0/tty/uevent:DEVNAME=3DttyS0 /sys/bus/serial-base/devices/serial8250:0.1/tty/uevent:DEVNAME=3DttyS1 /sys/bus/serial-base/devices/serial8250:0.2/tty/uevent:DEVNAME=3DttyS2 /sys/bus/serial-base/devices/serial8250:0.3/tty/uevent:DEVNAME=3DttyS3 With this change, we can add /dev/serial/by-id symlinks for the serial core port device instances. This allows using hardware based port addressing in addition to the legacy ttyS style naming. The serial core port naming is DEVNAME:0.0, such as the 00:04:0.0 above. The 0.0 above are serial core controller id and port id. The port id and controller id are typically both zero unless the serial port hardware controller has multiple controllers or ports. Signed-off-by: Tony Lindgren --- drivers/tty/serial/serial_core.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_c= ore.c --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -3328,6 +3328,8 @@ static int serial_core_port_device_add(struct serial_= ctrl_device *ctrl_dev, int serial_core_register_port(struct uart_driver *drv, struct uart_port *p= ort) { struct serial_ctrl_device *ctrl_dev, *new_ctrl_dev =3D NULL; + struct uart_match match =3D {port, drv}; + struct device *tty_dev; int ret; =20 mutex_lock(&port_mutex); @@ -3368,10 +3370,22 @@ int serial_core_register_port(struct uart_driver *d= rv, struct uart_port *port) =20 port->flags &=3D ~UPF_DEAD; =20 + tty_dev =3D device_find_child(port->dev, &match, serial_match_port); + if (tty_dev) { + ret =3D sysfs_create_link(&port->port_dev->dev.kobj, &tty_dev->kobj, + "tty"); + put_device(tty_dev); + if (ret) + goto err_remove_port; + } + mutex_unlock(&port_mutex); =20 return 0; =20 +err_remove_port: + serial_core_remove_one_port(drv, port); + err_unregister_port_dev: serial_base_port_device_remove(port->port_dev); =20 @@ -3393,12 +3407,20 @@ void serial_core_unregister_port(struct uart_driver= *drv, struct uart_port *port struct device *phys_dev =3D port->dev; struct serial_port_device *port_dev =3D port->port_dev; struct serial_ctrl_device *ctrl_dev =3D serial_core_get_ctrl_dev(port_dev= ); + struct uart_match match =3D {port, drv}; int ctrl_id =3D port->ctrl_id; + struct device *tty_dev; =20 mutex_lock(&port_mutex); =20 port->flags |=3D UPF_DEAD; =20 + tty_dev =3D device_find_child(port->dev, &match, serial_match_port); + if (tty_dev) { + sysfs_remove_link(&port->port_dev->dev.kobj, "tty"); + put_device(tty_dev); + } + serial_core_remove_one_port(drv, port); =20 /* Note that struct uart_port *port is no longer valid at this point */ --=20 2.42.0