Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp116032rdb; Mon, 4 Dec 2023 23:34:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IHXJpt7Rf4Fsv9C0d+1TsVkIYbZrtwkTqKNfVcteZY0u7WpAK/UlTCPSkRZbfZIio2VaqNA X-Received: by 2002:a05:6a00:1c9c:b0:6ce:3b75:4998 with SMTP id y28-20020a056a001c9c00b006ce3b754998mr933656pfw.16.1701761679882; Mon, 04 Dec 2023 23:34:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701761679; cv=none; d=google.com; s=arc-20160816; b=T0mYCw0leb3d6Ap4RGo+5K81zkEfEjVBCo+kKDlUeGbF3UmtPvzSKvLxJcJ24KFQFP WwrSD1ndsrOqDc0swMObeneaW2SeqWQhoEBMkY9Mee/MM9td0McrLIMdQ0DoD3e6/YUS P5KvLJ5m8oj/D/P8oGwvgUxv/sDF+dyUEFb9DusK9ZD/5lVjOfZ6GR3CSKO+9zzxiNKv YD6wSrNGQPFNbe+aXjpM/v+0CJtl4uPRjhIf8Y5QfVptRqGu7lEmLRRCipIN/z+DDIOx WKYm9pPHyorJxqOYFlH4cp6EQXMglC1zZBIzMxBcrAg37kKMr1s/ZVd8J0U8QKBZDocB trbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0sV2eE/0/qpqClT/7Gu5C5lmb80ZWF2ytmdtjmp0qvM=; fh=OQrZIrddo55Tm+afxTOsnGwFgD38y2NNd7tU+1OGSCE=; b=DJRoJu5vAnz4UoSU5EW3pnL4j568bts22bGtP6QEVGhtpNeATHRtq5p1hgcJqDq3uG glBgotQwxwRotzYHDjNEZL3GgiueEGR/UHg84ce+agv/tdB2OwpyPYEQIr0FtUdenFvP hB4PDppQYGSRvVJhd+obGwPlrcZifYDtPg17xx4XrEJ7kwwlTzico38PCEOACj4IlkWL lfxddSbbfh+IPOOfhl3svhlX36mIoczoXvCTiE+njggF7Kl7o7R5vSD6bOLNory85hr8 JhAAW3RDxMOzY8BKTv6hXOY4NU0y6OH1gndO/cD2uX/UJ/hXdhc0TJUTxdPA0LtmBinY Uhjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@atomide.com header.s=25mailst header.b=CbT9QWaE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id u28-20020a056a00099c00b006ce06fc9968si6467290pfg.233.2023.12.04.23.34.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 23:34:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=fail header.i=@atomide.com header.s=25mailst header.b=CbT9QWaE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id CE6208097145; Mon, 4 Dec 2023 23:34:36 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234796AbjLEHeT (ORCPT + 99 others); Tue, 5 Dec 2023 02:34:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231705AbjLEHeR (ORCPT ); Tue, 5 Dec 2023 02:34:17 -0500 Received: from mail5.25mail.st (mail5.25mail.st [74.50.62.9]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11B72188; Mon, 4 Dec 2023 23:34:23 -0800 (PST) Received: from localhost (91-158-86-216.elisa-laajakaista.fi [91.158.86.216]) by mail5.25mail.st (Postfix) with ESMTPSA id 7431C60354; Tue, 5 Dec 2023 07:33:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=atomide.com; s=25mailst; t=1701761662; bh=4xP9cJSfauOpucthPnTkf58g4HcyVjB8aEQlHWQSQnA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CbT9QWaEYFQLKrxaTbjS4aBKV0kxVM1AE//mdrOCn2xvsSTY+ec36le42LxOTqzfL hdBZx6Qs151OA27uuifHl0cbtcFYqyZ5Qn56Thmg6gv8/hnez9iX0mGniY+1bU+5Bz PmeuJDdQV14RAeQIxGjrPLUm8JDr7pn6Gi3u7WsGPoN2qoAQ6XAbhi9O/lGOJ3oEIU 1+C1yEHLF9P0osH1T4yCNF7D9NeivQRcM3kmLl409vRoorenFTPzpfvIr7Qi6XUF8I dqEHYpglGEei3u8/1pTL94Qk9ZGU4yjKseUf83bq4dSw7OfSnOKqWkRHge6B3GTWwW KzVY5nHkClNHQ== From: Tony Lindgren To: Greg Kroah-Hartman , Jiri Slaby , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky Cc: "David S . Miller" , 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 v4 1/4] printk: Save console options for add_preferred_console_match() Date: Tue, 5 Dec 2023 09:32:33 +0200 Message-ID: <20231205073255.20562-2-tony@atomide.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231205073255.20562-1-tony@atomide.com> References: <20231205073255.20562-1-tony@atomide.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 04 Dec 2023 23:34:36 -0800 (PST) Driver subsystems may need to translate the preferred console name to the character device name used. We already do some of this in console_setup() with a few hardcoded names, but that does not scale well. The console options are parsed early in console_setup(), and the consoles are added with __add_preferred_console(). At this point we don't know much about the character device names and device drivers getting probed. To allow drivers subsystems to set up a preferred console, let's save the kernel command line console options. To add a preferred console from a driver subsystem with optional character device name translation, let's add a new function add_preferred_console_match(). This allows the serial core layer to support console=DEVNAME:0.0 style hardware based addressing in addition to the current console=ttyS0 style naming. And we can start moving console_setup() character device parsing to the driver subsystem specific code. We use a separate array from the console_cmdline array as the character device name and index may be unknown at the console_setup() time. And eventually there's no need to call __add_preferred_console() until the character device name and index are known. Adding the console name in addition to the character device name, and a flag for an added console, could be added to the struct console_cmdline. And the console_cmdline array handling could be modified accordingly. But that complicates things compared saving the console options, and then adding the consoles when the subsystems handling the consoles are ready. Signed-off-by: Tony Lindgren --- include/linux/printk.h | 3 + kernel/printk/Makefile | 2 +- kernel/printk/conopt.c | 128 ++++++++++++++++++++++++++++++++ kernel/printk/console_cmdline.h | 6 ++ kernel/printk/printk.c | 14 +++- 5 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 kernel/printk/conopt.c diff --git a/include/linux/printk.h b/include/linux/printk.h --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -60,6 +60,9 @@ static inline const char *printk_skip_headers(const char *buffer) #define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT #define CONSOLE_LOGLEVEL_QUIET CONFIG_CONSOLE_LOGLEVEL_QUIET +int add_preferred_console_match(const char *match, const char *name, + const short idx); + extern int console_printk[]; #define console_loglevel (console_printk[0]) diff --git a/kernel/printk/Makefile b/kernel/printk/Makefile --- a/kernel/printk/Makefile +++ b/kernel/printk/Makefile @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-y = printk.o +obj-y = printk.o conopt.o obj-$(CONFIG_PRINTK) += printk_safe.o nbcon.o obj-$(CONFIG_A11Y_BRAILLE_CONSOLE) += braille.o obj-$(CONFIG_PRINTK_INDEX) += index.o diff --git a/kernel/printk/conopt.c b/kernel/printk/conopt.c new file mode 100644 --- /dev/null +++ b/kernel/printk/conopt.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Kernel command line console options for hardware based addressing + * + * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/ + * Author: Tony Lindgren + */ + +#include +#include +#include + +#include + +#include "console_cmdline.h" + +/* + * Allow longer DEVNAME:0.0 style console naming such as abcd0000.serial:0.0 + * in addition to the legacy ttyS0 style naming. + */ +#define CONSOLE_NAME_MAX 32 + +#define CONSOLE_OPT_MAX 16 +#define CONSOLE_BRL_OPT_MAX 16 + +struct console_option { + char name[CONSOLE_NAME_MAX]; + char opt[CONSOLE_OPT_MAX]; + char brl_opt[CONSOLE_BRL_OPT_MAX]; +}; + +/* Updated only at console_setup() time, no locking needed */ +static struct console_option conopt[MAX_CMDLINECONSOLES]; + +/** + * console_opt_save - Saves kernel command line console option for driver use + * @str: Kernel command line console name and option + * @brl_opt: Braille console options + * + * Saves a kernel command line console option for driver subsystems to use for + * adding a preferred console during init. Called from console_setup() only. + * + * Return: 0 on success, negative error code on failure. + */ +int __init console_opt_save(const char *str, const char *brl_opt) +{ + struct console_option *con; + const char *opt = NULL; + size_t namelen, optlen; + int i; + + namelen = strcspn(str, ","); + if (!namelen) + return -EINVAL; + + optlen = strlen(str) - namelen; + if (optlen > 1) + opt = str + namelen + 1; + + if (namelen >= CONSOLE_NAME_MAX || optlen >= CONSOLE_OPT_MAX) + return -EINVAL; + + for (i = 0; i < MAX_CMDLINECONSOLES; i++) { + con = &conopt[i]; + + if (con->name[0]) { + if (!strncmp(str, con->name, namelen)) + return 0; + continue; + } + + strscpy(con->name, str, namelen + 1); + if (opt) + strscpy(con->opt, opt, optlen + 1); + + if (brl_opt) + strscpy(con->brl_opt, brl_opt, CONSOLE_BRL_OPT_MAX); + + return 0; + } + + return -ENOMEM; +} + +static struct console_option *console_opt_find(const char *name) +{ + struct console_option *con; + int i; + + for (i = 0; i < MAX_CMDLINECONSOLES; i++) { + con = &conopt[i]; + if (!strcmp(name, con->name)) + return con; + } + + return NULL; +} + +/** + * add_preferred_console_match - Adds a preferred console if a match is found + * @match: Expected console on kernel command line, such as console=DEVNAME:0.0 + * @name: Name of the console character device to add such as ttyS + * @idx: Index for the console + * + * Allows driver subsystems to add a console after translating the command + * line name to the character device name used for the console. Options are + * added automatically based on the kernel command line. Duplicate preferred + * consoles are ignored by __add_preferred_console(). + * + * Return: 0 on success, negative error code on failure. + */ +int add_preferred_console_match(const char *match, const char *name, + const short idx) +{ + struct console_option *con; + + if (!match || !strlen(match) || !name || !strlen(name) || + idx < 0) + return -EINVAL; + + con = console_opt_find(match); + if (!con) + return -ENOENT; + + console_opt_add_preferred_console(name, idx, con->opt, con->brl_opt); + + return 0; +} diff --git a/kernel/printk/console_cmdline.h b/kernel/printk/console_cmdline.h --- a/kernel/printk/console_cmdline.h +++ b/kernel/printk/console_cmdline.h @@ -2,6 +2,12 @@ #ifndef _CONSOLE_CMDLINE_H #define _CONSOLE_CMDLINE_H +#define MAX_CMDLINECONSOLES 8 + +int console_opt_save(const char *str, const char *brl_opt); +int console_opt_add_preferred_console(const char *name, const short idx, + char *options, char *brl_options); + struct console_cmdline { char name[16]; /* Name of the driver */ diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -360,9 +360,6 @@ static int console_locked; /* * Array of consoles built from command line options (console=) */ - -#define MAX_CMDLINECONSOLES 8 - static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES]; static int preferred_console = -1; @@ -2458,6 +2455,10 @@ static int __init console_setup(char *str) if (_braille_console_setup(&str, &brl_options)) return 1; + /* Save the console for driver subsystem use */ + if (console_opt_save(str, brl_options)) + return 1; + /* * Decode str into name, index, options. */ @@ -2488,6 +2489,13 @@ static int __init console_setup(char *str) } __setup("console=", console_setup); +/* Only called from add_preferred_console_match() */ +int console_opt_add_preferred_console(const char *name, const short idx, + char *options, char *brl_options) +{ + return __add_preferred_console(name, idx, options, brl_options, true); +} + /** * add_preferred_console - add a device to the list of preferred consoles. * @name: device name -- 2.43.0