From patchwork Tue Mar 22 20:59:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Anderson X-Patchwork-Id: 1608365 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=seco.com header.i=@seco.com header.a=rsa-sha256 header.s=selector1 header.b=M+0Tly/j; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KNPBW3c3Zz9s1l for ; Wed, 23 Mar 2022 08:04:51 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8FB6D83F70; Tue, 22 Mar 2022 22:02:56 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=seco.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=seco.com header.i=@seco.com header.b="M+0Tly/j"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B00D183C97; Tue, 22 Mar 2022 22:01:51 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from EUR02-AM5-obe.outbound.protection.outlook.com (mail-am5eur02on0616.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe07::616]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 4EBCE83BA2 for ; Tue, 22 Mar 2022 22:00:38 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=seco.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sean.anderson@seco.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=I+RKGqkxJW3OEKxWhEZ25+TkWhCR3VW2wLUSG36DWCZ2+Ibb9KGIkJzIVtc1so0DcvU8VAv44FT+HxQm7iTTm3aY1KLcgkRKpqohXDZ8lDKtknIZbEKJBmXkn5qfechiEg5EMIVlJcJBMNMTgwDHpwp9V0sT3SEG0Y4Izmc7RBszWPXTvLqagsLS/1+S0mVu+JqLBrCTg3ARES4qluaJo8/N0RTXLm0ltdmIU/xHiqR2IhUG2n/ksjNF1I8f68NBbROnFZLMuKMs6lLV6CD3P6ecF5vNL2azwGX8ROes60JnX91i92beX8Tw6zJ90ZHrsAmsE4wWspsMjxu9K6TCkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=S01kINpRzxmlexcAV9Yn8JVg/6kYr+zX3AQHHeAjwQ8=; b=EdFI6NbXUv61YOMe1b0wLxZTxh5F43UEOr8GvhendOJ5wohRLnzCOEUzIEA4OO0rlmts7N9scef3IJtAClexMLqU5UYqU98xXvVMcsSYNyvGiGBzHLnotUaecJbpwMcMf8VDjCavMfyq0SNSSmq7ChiMEWj9hzxHwz2t9TCk5+vuH7Ch4l/2dLlmF+MHTb71acr46RIA8qMDOxehIFbaDEYlfDFasW/uYj3kDrqCFFNT7M2NJR6IJRmgh96tBAln/yKTNdb1B5qNHUkr40cggaOA5Z4Cy4DIfm3ceaDPx5sjk8fHMQJJBm68gQwNa65xXlwtnnswF4rK4YPVSjKBqg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=seco.com; dmarc=pass action=none header.from=seco.com; dkim=pass header.d=seco.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seco.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=S01kINpRzxmlexcAV9Yn8JVg/6kYr+zX3AQHHeAjwQ8=; b=M+0Tly/jvx2NRnb7+kaUMaO4FJO/vz0hptz8jpokdwBuCLSZWpswNBCiniHNlRUyW8h7pGAs7H0+oj6wHK7YdEpQ/p4rQJhv72+pImVjEObY9K8GJa0l5xlbKEYFn20864Bav2aBYy05JHEYwDEy8S2Gjyj+SHrR0+yIruvj/JhykES7O5nBdzQ1DuwcXG8E7vfRA19w3ZZJV/NTDUB13NBwqMgH/zJ/s6RO00X7XfV8CC875huswqRiRDSQgQy8sooD9RnAP6qQamXRvCeGGfTaeQU8V5mllaPwRqWNELh1g1vYMM5UNYSytG0+WACpnPlK2eqTQNcNvFy2p+yyeA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=seco.com; Received: from DB7PR03MB4972.eurprd03.prod.outlook.com (2603:10a6:10:7d::22) by VI1PR03MB4591.eurprd03.prod.outlook.com (2603:10a6:803:5f::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5081.23; Tue, 22 Mar 2022 21:00:37 +0000 Received: from DB7PR03MB4972.eurprd03.prod.outlook.com ([fe80::2414:8ad5:9fd6:3bff]) by DB7PR03MB4972.eurprd03.prod.outlook.com ([fe80::2414:8ad5:9fd6:3bff%5]) with mapi id 15.20.5081.023; Tue, 22 Mar 2022 21:00:37 +0000 From: Sean Anderson To: Tom Rini Cc: Linus Walleij , Liviu Dudau , Andre Przywara , Simon Glass , u-boot@lists.denx.de, Sean Anderson Subject: [PATCH v3 26/29] serial: dm: Add support for puts Date: Tue, 22 Mar 2022 16:59:34 -0400 Message-Id: <20220322205938.1721846-27-sean.anderson@seco.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220322205938.1721846-1-sean.anderson@seco.com> References: <20220322205938.1721846-1-sean.anderson@seco.com> X-ClientProxiedBy: MN2PR20CA0012.namprd20.prod.outlook.com (2603:10b6:208:e8::25) To DB7PR03MB4972.eurprd03.prod.outlook.com (2603:10a6:10:7d::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 77c0aabf-b87d-4922-04e5-08da0c4703ac X-MS-TrafficTypeDiagnostic: VI1PR03MB4591:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: jgnWuZzp8sLmZNRfzontv96oGkpaTU9O5SYeYt1BKWVKzgBGtyYtZc9H22d+27DpMoMKVxDRB+jXnwqp3ua+tCV/yI82Nwd0ocnM2CNU8a11uie2RX/7ORQoAVPdiOFzFsNHuoMyns9DOYQDPc/z5mJ8Ds2FTdjaPITSJ5ZWjew7TUl1T2VHFbtrleLuaUVkmQc1jtqx23BsTh5nP8GhBoB+JSOJGqjNTxV/E190sE1BgmCRKwSxWCVjKcl87u4crchwpT18D3gCKfWDP8nPbPQfftNEqlYfYMenaRBzcXjiAEl2w9RWmTGWprc/KQ6EpoIZsg2L3QRk0DzrsXhQ9SXj84CXiEeXkdCed7op9tmSUDgu6/j9G4zYJnZ7KjKOfO5oJW8WKxAjRtVWg/NjoUEucIRMnhvTxCC3R0dIXgW+0rv+F+xlgw+gD5ViHomG8UlBcSh1rjDXLVQja9iuJ852GetOcSp+GrTcksCX1y65gqDn2wWHJoChNIDqmyNcKRc49vv3nxujgcPismocybRvhYrnJyWcVrG55Q+nE7PKx0W8q7wEUSpEYFYW03yyrqRApBJHJtkCi/UooB7vY8Acr5+Z1zmNT1Ad/ODK+hAJw+fB645NYOiyCBvux7+Gyjfu828MY9jEQoeZdkFIj5A8ZqKAeqk8aVfZxb/UPsNEtBvsla2enH5+Fl2Rq2HAnYMewgNn6EVVUiVej3mcQw== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DB7PR03MB4972.eurprd03.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(366004)(2906002)(83380400001)(6486002)(186003)(26005)(107886003)(38100700002)(38350700002)(2616005)(1076003)(6506007)(6512007)(6916009)(36756003)(52116002)(54906003)(316002)(4326008)(5660300002)(8676002)(66556008)(66476007)(508600001)(86362001)(66946007)(8936002)(6666004)(44832011); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: evHVx8vZnWNSxZfH3XUhbsn5WOjno2VFzmw27I+HQkKSHPokHwbewrTVJrvrifF/Be5dr6WIzGxbc6KYKKkinMOG1A26kaxGPvGFcWuFtIL9Ie/BAmNBmR6ZatNym1LZaRe44RffeXOgP9e1sCTusS4lCw8iE7ZfLL1KT9tZrFxloBUjzsP/Y1zMu/I9ZSRw3dNICMCZ4CxYsaRz9mHOhMOB9lzXoeIy2gGPVSrF2PEPAY9akaGjz/7Hyvv+JYTbuI1Hqo7Ls54waRvnwlVGuDqi85LhD7426rV/gGPEDf/fECuyGQ0V+L8vZ0++OMPFM5UzNf6z6eazA5Hj74vr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WLvzATJsPXjezzSqaF1/iCh0+7O3a2bVsAhrnx6QxlyfVYbxraibEATxT8xol+kqrWgC61l8iZ+AsZ1AbpHgpw== X-OriginatorOrg: seco.com X-MS-Exchange-CrossTenant-Network-Message-Id: 77c0aabf-b87d-4922-04e5-08da0c4703ac X-MS-Exchange-CrossTenant-AuthSource: DB7PR03MB4972.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Mar 2022 21:00:37.1725 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bebe97c3-6438-442e-ade3-ff17aa50e733 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 6nr7Nrg5fJfBZH7b5U2yEF6iiTVPgWBCypGEB5hCDR6U37o6PgG0KbmW0qANnDSRVYDAsfvqexpldguzqbsa1w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR03MB4591 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean Some serial drivers can be vastly more efficient when printing multiple characters at once. Non-DM serial has had a puts option for these sorts of drivers; implement it for DM serial as well. Because we have to add carriage returns, we can't just pass the whole string directly to the serial driver. Instead, we print up to the newline, then print a carriage return, and then continue on. This is less efficient, but it is better than printing each character individually. It also avoids having to allocate memory just to add a few characters. Drivers may perform short writes (such as filling a FIFO) and return the number of characters written in len. We loop over them in the same way that _serial_putc loops over putc. This results in around sizeof(void *) growth for all boards with DM_SERIAL. The full implementation takes around 140 bytes. Signed-off-by: Sean Anderson Reviewed-by: Simon Glass --- Changes in v3: - Add a config for puts to reduce the impact on the (vast majority) of boards which don't need it. - Fix null pointer dereference in _serial_puts caused by a missing return. - Make puts return the number of characters written on success, instead of reusing the len parameter. Changes in v2: - New drivers/serial/Kconfig | 13 +++++++++++++ drivers/serial/serial-uclass.c | 26 ++++++++++++++++++++++++-- include/serial.h | 18 ++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig index cc20759505..1a109b2411 100644 --- a/drivers/serial/Kconfig +++ b/drivers/serial/Kconfig @@ -133,6 +133,19 @@ config SERIAL_RX_BUFFER_SIZE help The size of the RX buffer (needs to be power of 2) +config SERIAL_PUTS + bool "Enable printing strings all at once" + depends on DM_SERIAL + help + Some serial drivers are much more efficient when printing multiple + characters at once rather than printing characters individually. This + can be because they can load a fifo, or because individual print + calls have a constant overhead. With this option set, the serial + subsystem will try to provide serial drivers with as many characters + at once as possible, instead of printing characters one by one. Most + serial drivers do not need this config to print efficiently. If + unsure, say N. + config SERIAL_SEARCH_ALL bool "Search for serial devices after default one failed" depends on DM_SERIAL diff --git a/drivers/serial/serial-uclass.c b/drivers/serial/serial-uclass.c index f30f352bd7..10d6b800e2 100644 --- a/drivers/serial/serial-uclass.c +++ b/drivers/serial/serial-uclass.c @@ -200,8 +200,30 @@ static void _serial_putc(struct udevice *dev, char ch) static void _serial_puts(struct udevice *dev, const char *str) { - while (*str) - _serial_putc(dev, *str++); + struct dm_serial_ops *ops = serial_get_ops(dev); + + if (!CONFIG_IS_ENABLED(SERIAL_PUTS) || !ops->puts) { + while (*str) + _serial_putc(dev, *str++); + return; + } + + do { + const char *newline = strchrnul(str, '\n'); + size_t len = newline - str + !!*newline; + + do { + ssize_t written = ops->puts(dev, str, len); + + if (written < 0) + return; + str += written; + len -= written; + } while (len); + + if (*newline) + _serial_putc(dev, '\r'); + } while (*str); } static int __serial_getc(struct udevice *dev) diff --git a/include/serial.h b/include/serial.h index 2681d26c82..8c2e7adbc3 100644 --- a/include/serial.h +++ b/include/serial.h @@ -195,6 +195,24 @@ struct dm_serial_ops { * @return 0 if OK, -ve on error */ int (*putc)(struct udevice *dev, const char ch); + /** + * puts() - Write a string + * + * This writes a string. This function should be implemented only if + * writing multiple characters at once is more performant than just + * calling putc() in a loop. + * + * If the whole string cannot be written at once, then this function + * should return the number of characters written. Returning a negative + * error code implies that no characters were written. If this function + * returns 0, then it will be called again with the same arguments. + * + * @dev: Device pointer + * @s: The string to write + * @len: The length of the string to write. + * @return The number of characters written on success, or -ve on error + */ + ssize_t (*puts)(struct udevice *dev, const char *s, size_t len); /** * pending() - Check if input/output characters are waiting *